"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tasksRelations = exports.activitiesRelations = exports.leadsRelations = exports.activities = exports.notifications = exports.tasks = exports.leads = exports.monthlyTargets = exports.users = void 0;
const mysql_core_1 = require("drizzle-orm/mysql-core");
const drizzle_orm_1 = require("drizzle-orm");
exports.users = (0, mysql_core_1.mysqlTable)('users', {
    id: (0, mysql_core_1.varchar)('id', { length: 36 }).primaryKey(),
    email: (0, mysql_core_1.varchar)('email', { length: 255 }).notNull().unique(),
    password: (0, mysql_core_1.text)('password').notNull(),
    name: (0, mysql_core_1.varchar)('name', { length: 255 }),
    role: (0, mysql_core_1.varchar)('role', { length: 50 }).default('employee'),
    avatar: (0, mysql_core_1.text)('avatar'),
    targetMargin: (0, mysql_core_1.int)('target_margin'),
    createdAt: (0, mysql_core_1.timestamp)('created_at').defaultNow(),
});
exports.monthlyTargets = (0, mysql_core_1.mysqlTable)('monthly_targets', {
    id: (0, mysql_core_1.varchar)('id', { length: 36 }).primaryKey(),
    userId: (0, mysql_core_1.varchar)('user_id', { length: 36 }).notNull(),
    month: (0, mysql_core_1.varchar)('month', { length: 7 }).notNull(), // 'YYYY-MM'
    targetAmount: (0, mysql_core_1.int)('target_amount').notNull(),
    createdAt: (0, mysql_core_1.timestamp)('created_at').defaultNow(),
}, (table) => ({
    userMonthIdx: (0, mysql_core_1.index)('user_month_idx').on(table.userId, table.month),
}));
exports.leads = (0, mysql_core_1.mysqlTable)('leads', {
    id: (0, mysql_core_1.varchar)('id', { length: 36 }).primaryKey(),
    customerName: (0, mysql_core_1.varchar)('customer_name', { length: 255 }).notNull(),
    phone: (0, mysql_core_1.varchar)('phone', { length: 50 }),
    email: (0, mysql_core_1.varchar)('email', { length: 255 }),
    destination: (0, mysql_core_1.varchar)('destination', { length: 255 }),
    travelDate: (0, mysql_core_1.varchar)('travel_date', { length: 50 }), // keeping uniform with frontend string dates for now
    pax: (0, mysql_core_1.int)('pax'),
    budget: (0, mysql_core_1.int)('budget'),
    status: (0, mysql_core_1.varchar)('status', { length: 50 }).notNull(),
    assignedTo: (0, mysql_core_1.varchar)('assigned_to', { length: 36 }),
    createdAt: (0, mysql_core_1.timestamp)('created_at').defaultNow(),
    notes: (0, mysql_core_1.json)('notes'),
    // New fields
    travelType: (0, mysql_core_1.varchar)('travel_type', { length: 50 }),
    proposalCost: (0, mysql_core_1.int)('proposal_cost'),
    proposalValidity: (0, mysql_core_1.varchar)('proposal_validity', { length: 50 }),
    paymentStatus: (0, mysql_core_1.varchar)('payment_status', { length: 50 }),
    bookingRef: (0, mysql_core_1.varchar)('booking_ref', { length: 100 }),
    lostReason: (0, mysql_core_1.text)('lost_reason'),
    lostCompetitor: (0, mysql_core_1.varchar)('lost_competitor', { length: 255 }),
    discountGiven: (0, mysql_core_1.int)('discount_given'),
    revenue: (0, mysql_core_1.int)('revenue'),
    profitMargin: (0, mysql_core_1.int)('profit_margin'),
    lastContactedAt: (0, mysql_core_1.varchar)('last_contacted_at', { length: 50 }),
    followUpCount: (0, mysql_core_1.int)('follow_up_count').default(0),
    unsuccessfulAttempts: (0, mysql_core_1.int)('unsuccessful_attempts').default(0),
    priority: (0, mysql_core_1.varchar)('priority', { length: 20 }),
    revenuePotential: (0, mysql_core_1.int)('revenue_potential'),
    nextAction: (0, mysql_core_1.text)('next_action'),
    lastNote: (0, mysql_core_1.text)('last_note'),
    nextFollowUpDate: (0, mysql_core_1.varchar)('next_follow_up_date', { length: 50 }),
    source: (0, mysql_core_1.varchar)('source', { length: 100 }),
    totalActivityCount: (0, mysql_core_1.int)('total_activity_count').default(0),
    assignedAt: (0, mysql_core_1.varchar)('assigned_at', { length: 50 }),
    viewedAt: (0, mysql_core_1.varchar)('viewed_at', { length: 50 }),
    bookedAt: (0, mysql_core_1.varchar)('booked_at', { length: 50 }),
    clientNote: (0, mysql_core_1.text)('client_note'),
    // Approval Workflow Fields
    pendingFollowUpDate: (0, mysql_core_1.varchar)('pending_follow_up_date', { length: 50 }),
    pendingFollowUpRequestBy: (0, mysql_core_1.varchar)('pending_follow_up_request_by', { length: 36 }),
    // Itinerary Builder
    // itinerary: json('itinerary'),
}, (table) => ({
    createdIdx: (0, mysql_core_1.index)('created_idx').on(table.createdAt),
    statusIdx: (0, mysql_core_1.index)('status_idx').on(table.status),
    assignedIdx: (0, mysql_core_1.index)('assigned_idx').on(table.assignedTo),
}));
exports.tasks = (0, mysql_core_1.mysqlTable)('tasks', {
    id: (0, mysql_core_1.varchar)('id', { length: 36 }).primaryKey(),
    title: (0, mysql_core_1.varchar)('title', { length: 255 }).notNull(),
    dueDate: (0, mysql_core_1.varchar)('due_date', { length: 50 }),
    status: (0, mysql_core_1.varchar)('status', { length: 20 }).default('pending'),
    leadId: (0, mysql_core_1.varchar)('lead_id', { length: 36 }),
    createdAt: (0, mysql_core_1.timestamp)('created_at').defaultNow(),
}, (table) => ({
    leadIdIdx: (0, mysql_core_1.index)('lead_id_idx').on(table.leadId),
}));
exports.notifications = (0, mysql_core_1.mysqlTable)('notifications', {
    id: (0, mysql_core_1.varchar)('id', { length: 36 }).primaryKey(),
    userId: (0, mysql_core_1.varchar)('user_id', { length: 36 }).notNull(),
    type: (0, mysql_core_1.varchar)('type', { length: 50 }),
    message: (0, mysql_core_1.text)('message'),
    leadId: (0, mysql_core_1.varchar)('lead_id', { length: 36 }),
    read: (0, mysql_core_1.boolean)('read').default(false),
    createdAt: (0, mysql_core_1.timestamp)('created_at').defaultNow(),
}, (table) => ({
    userIdIdx: (0, mysql_core_1.index)('user_id_idx').on(table.userId),
}));
exports.activities = (0, mysql_core_1.mysqlTable)('activities', {
    id: (0, mysql_core_1.varchar)('id', { length: 36 }).primaryKey(),
    type: (0, mysql_core_1.varchar)('type', { length: 50 }),
    description: (0, mysql_core_1.text)('description'),
    entityId: (0, mysql_core_1.varchar)('entity_id', { length: 36 }),
    entityType: (0, mysql_core_1.varchar)('entity_type', { length: 50 }),
    metadata: (0, mysql_core_1.json)('metadata'),
    createdAt: (0, mysql_core_1.timestamp)('created_at').defaultNow(),
    userId: (0, mysql_core_1.varchar)('user_id', { length: 36 }),
}, (table) => ({
    entityIdIdx: (0, mysql_core_1.index)('entity_id_idx').on(table.entityId),
}));
exports.leadsRelations = (0, drizzle_orm_1.relations)(exports.leads, ({ many }) => ({
    activities: many(exports.activities),
    tasks: many(exports.tasks),
}));
exports.activitiesRelations = (0, drizzle_orm_1.relations)(exports.activities, ({ one }) => ({
    lead: one(exports.leads, {
        fields: [exports.activities.entityId],
        references: [exports.leads.id],
    }),
}));
exports.tasksRelations = (0, drizzle_orm_1.relations)(exports.tasks, ({ one }) => ({
    lead: one(exports.leads, {
        fields: [exports.tasks.leadId],
        references: [exports.leads.id],
    }),
}));
