import {sql} from "drizzle-orm";
import {boolean,index,integer,pgTable,serial,text} from "drizzle-orm/pg-core";
const created=()=>text("created_at").notNull().default(sql`CURRENT_TIMESTAMP::text`);
export const vehicleTypes=pgTable("vehicle_types",{id:serial("id").primaryKey(),brand:text("brand").notNull(),name:text("name").notNull(),category:text("category").notNull().default("سواری"),imageUrl:text("image_url").notNull().default(""),isActive:boolean("is_active").notNull().default(true),createdAt:created()},t=>[index("idx_vehicle_types_active").on(t.isActive)]);
export const vehicles=pgTable("vehicles",{id:serial("id").primaryKey(),typeId:integer("type_id").references(()=>vehicleTypes.id),title:text("title").notNull(),model:text("model").notNull().default(""),plate:text("plate").notNull().unique(),vin:text("vin").notNull().default(""),year:integer("year"),color:text("color").notNull().default(""),mileage:integer("mileage").notNull().default(0),status:text("status").notNull().default("ready"),inspectionIntervalDays:integer("inspection_interval_days").notNull().default(30),notes:text("notes").notNull().default(""),createdAt:created()},t=>[index("idx_vehicles_status").on(t.status)]);
export const drivers=pgTable("drivers",{id:serial("id").primaryKey(),fullName:text("full_name").notNull(),nationalId:text("national_id").notNull().unique(),phone:text("phone").notNull(),licenseNo:text("license_no").notNull(),licenseExpiry:text("license_expiry").notNull().default(""),status:text("status").notNull().default("active"),notes:text("notes").notNull().default(""),createdAt:created()},t=>[index("idx_drivers_status").on(t.status)]);
export const assignments=pgTable("assignments",{id:serial("id").primaryKey(),vehicleId:integer("vehicle_id").notNull().references(()=>vehicles.id),driverId:integer("driver_id").notNull().references(()=>drivers.id),startAt:text("start_at").notNull(),expectedReturnAt:text("expected_return_at"),endAt:text("end_at"),assignmentType:text("assignment_type").notNull().default("long_term"),purpose:text("purpose").notNull().default(""),destination:text("destination").notNull().default(""),startMileage:integer("start_mileage").notNull().default(0),endMileage:integer("end_mileage"),status:text("status").notNull().default("active"),returnRequestedAt:text("return_requested_at"),returnedApprovedAt:text("returned_approved_at"),createdAt:created()},t=>[index("idx_assignments_vehicle_status").on(t.vehicleId,t.status),index("idx_assignments_driver_status").on(t.driverId,t.status)]);
export const vehicleRequests=pgTable("vehicle_requests",{id:serial("id").primaryKey(),vehicleId:integer("vehicle_id").notNull().references(()=>vehicles.id),driverId:integer("driver_id").notNull().references(()=>drivers.id),purpose:text("purpose").notNull().default(""),destination:text("destination").notNull().default(""),requestedAt:text("requested_at").notNull().default(sql`CURRENT_TIMESTAMP::text`),status:text("status").notNull().default("pending"),supervisorNote:text("supervisor_note").notNull().default(""),resolvedAt:text("resolved_at")},t=>[index("idx_vehicle_requests_driver_status").on(t.driverId,t.status),index("idx_vehicle_requests_vehicle_status").on(t.vehicleId,t.status)]);
export const maintenance=pgTable("maintenance",{id:serial("id").primaryKey(),vehicleId:integer("vehicle_id").notNull().references(()=>vehicles.id),title:text("title").notNull(),dueDate:text("due_date").notNull(),dueMileage:integer("due_mileage"),cost:integer("cost").notNull().default(0),vendor:text("vendor").notNull().default(""),notes:text("notes").notNull().default(""),status:text("status").notNull().default("scheduled"),completedAt:text("completed_at"),createdAt:created()},t=>[index("idx_maintenance_vehicle_status").on(t.vehicleId,t.status),index("idx_maintenance_due_date").on(t.dueDate)]);
export const approvals=pgTable("approvals",{id:serial("id").primaryKey(),type:text("type").notNull(),referenceId:integer("reference_id").notNull(),title:text("title").notNull(),requestedBy:text("requested_by").notNull().default("مدیر ناوگان"),status:text("status").notNull().default("pending"),supervisorNote:text("supervisor_note").notNull().default(""),resolvedAt:text("resolved_at"),createdAt:created()},t=>[index("idx_approvals_status").on(t.status)]);
export const inspections=pgTable("inspections",{id:serial("id").primaryKey(),code:text("code").notNull().unique(),vehicleId:integer("vehicle_id").notNull().references(()=>vehicles.id),driverId:integer("driver_id").references(()=>drivers.id),assignmentId:integer("assignment_id").references(()=>assignments.id),inspectorUserId:text("inspector_user_id").notNull(),inspectorName:text("inspector_name").notNull(),inspectedAt:text("inspected_at").notNull(),odometer:integer("odometer").notNull(),technicalCertificateValid:boolean("technical_certificate_valid").notNull(),driverLicenseValid:boolean("driver_license_valid").notNull(),supervisorStatus:text("supervisor_status").notNull().default("approved"),managerStatus:text("manager_status").notNull().default("pending"),managerNote:text("manager_note").notNull().default(""),nextDueAt:text("next_due_at").notNull(),notes:text("notes").notNull().default(""),createdAt:created()},t=>[index("idx_inspections_vehicle_date").on(t.vehicleId,t.inspectedAt),index("idx_inspections_manager_status").on(t.managerStatus)]);
export const inspectionItems=pgTable("inspection_items",{id:serial("id").primaryKey(),inspectionId:integer("inspection_id").notNull().references(()=>inspections.id,{onDelete:"cascade"}),groupKey:text("group_key").notNull(),itemKey:text("item_key").notNull(),label:text("label").notNull(),status:text("status").notNull(),note:text("note").notNull().default("")},t=>[index("idx_inspection_items_inspection").on(t.inspectionId)]);
export const inspectionPhotos=pgTable("inspection_photos",{id:serial("id").primaryKey(),inspectionId:integer("inspection_id").notNull().references(()=>inspections.id,{onDelete:"cascade"}),kind:text("kind").notNull(),filePath:text("file_path").notNull(),originalName:text("original_name").notNull(),mimeType:text("mime_type").notNull(),sizeBytes:integer("size_bytes").notNull(),createdAt:created()},t=>[index("idx_inspection_photos_inspection").on(t.inspectionId)]);
export const tickets=pgTable("tickets",{id:serial("id").primaryKey(),code:text("code").notNull().unique(),subject:text("subject").notNull(),category:text("category").notNull().default("general"),priority:text("priority").notNull().default("normal"),status:text("status").notNull().default("open"),vehicleId:integer("vehicle_id").references(()=>vehicles.id),driverId:integer("driver_id").references(()=>drivers.id),assignmentId:integer("assignment_id").references(()=>assignments.id),reporter:text("reporter").notNull().default("مدیر ناوگان"),assignee:text("assignee").notNull().default("سوپروایزر"),description:text("description").notNull().default(""),closedAt:text("closed_at"),createdAt:created(),updatedAt:text("updated_at").notNull().default(sql`CURRENT_TIMESTAMP::text`)},t=>[index("idx_tickets_status_priority").on(t.status,t.priority),index("idx_tickets_vehicle_id").on(t.vehicleId)]);
export const ticketMessages=pgTable("ticket_messages",{id:serial("id").primaryKey(),ticketId:integer("ticket_id").notNull().references(()=>tickets.id),author:text("author").notNull(),authorRole:text("author_role").notNull().default("management"),body:text("body").notNull(),createdAt:created()},t=>[index("idx_ticket_messages_ticket_id").on(t.ticketId)]);
export const smsLogs=pgTable("sms_logs",{id:serial("id").primaryKey(),event:text("event").notNull(),receptor:text("receptor").notNull(),template:text("template").notNull().default(""),token:text("token").notNull().default(""),status:text("status").notNull().default("unconfigured"),providerMessageId:text("provider_message_id"),responseMessage:text("response_message").notNull().default(""),createdAt:created()},t=>[index("idx_sms_logs_status_created").on(t.status,t.createdAt)]);
export const users=pgTable("users",{id:text("id").primaryKey(),username:text("username").notNull().unique(),passwordHash:text("password_hash").notNull(),fullName:text("full_name").notNull(),role:text("role").notNull(),driverId:integer("driver_id").references(()=>drivers.id),isActive:boolean("is_active").notNull().default(true),mustChangePassword:boolean("must_change_password").notNull().default(true),createdAt:created()},t=>[index("idx_users_role_active").on(t.role,t.isActive)]);
export const sessions=pgTable("sessions",{tokenHash:text("token_hash").primaryKey(),userId:text("user_id").notNull().references(()=>users.id,{onDelete:"cascade"}),expiresAt:text("expires_at").notNull(),createdAt:created()},t=>[index("idx_sessions_user_id").on(t.userId),index("idx_sessions_expires_at").on(t.expiresAt)]);
