CREATE TABLE "approvals" (
	"id" serial PRIMARY KEY NOT NULL,
	"type" text NOT NULL,
	"reference_id" integer NOT NULL,
	"title" text NOT NULL,
	"requested_by" text DEFAULT 'مدیر ناوگان' NOT NULL,
	"status" text DEFAULT 'pending' NOT NULL,
	"supervisor_note" text DEFAULT '' NOT NULL,
	"resolved_at" text,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "assignments" (
	"id" serial PRIMARY KEY NOT NULL,
	"vehicle_id" integer NOT NULL,
	"driver_id" integer NOT NULL,
	"start_at" text NOT NULL,
	"expected_return_at" text,
	"end_at" text,
	"assignment_type" text DEFAULT 'long_term' NOT NULL,
	"purpose" text DEFAULT '' NOT NULL,
	"destination" text DEFAULT '' NOT NULL,
	"start_mileage" integer DEFAULT 0 NOT NULL,
	"end_mileage" integer,
	"status" text DEFAULT 'active' NOT NULL,
	"return_requested_at" text,
	"returned_approved_at" text,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "drivers" (
	"id" serial PRIMARY KEY NOT NULL,
	"full_name" text NOT NULL,
	"national_id" text NOT NULL,
	"phone" text NOT NULL,
	"license_no" text NOT NULL,
	"license_expiry" text DEFAULT '' NOT NULL,
	"status" text DEFAULT 'active' NOT NULL,
	"notes" text DEFAULT '' NOT NULL,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL,
	CONSTRAINT "drivers_national_id_unique" UNIQUE("national_id")
);
--> statement-breakpoint
CREATE TABLE "inspection_items" (
	"id" serial PRIMARY KEY NOT NULL,
	"inspection_id" integer NOT NULL,
	"group_key" text NOT NULL,
	"item_key" text NOT NULL,
	"label" text NOT NULL,
	"status" text NOT NULL,
	"note" text DEFAULT '' NOT NULL
);
--> statement-breakpoint
CREATE TABLE "inspection_photos" (
	"id" serial PRIMARY KEY NOT NULL,
	"inspection_id" integer NOT NULL,
	"kind" text NOT NULL,
	"file_path" text NOT NULL,
	"original_name" text NOT NULL,
	"mime_type" text NOT NULL,
	"size_bytes" integer NOT NULL,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "inspections" (
	"id" serial PRIMARY KEY NOT NULL,
	"code" text NOT NULL,
	"vehicle_id" integer NOT NULL,
	"driver_id" integer,
	"assignment_id" integer,
	"inspector_user_id" text NOT NULL,
	"inspector_name" text NOT NULL,
	"inspected_at" text NOT NULL,
	"odometer" integer NOT NULL,
	"technical_certificate_valid" boolean NOT NULL,
	"driver_license_valid" boolean NOT NULL,
	"supervisor_status" text DEFAULT 'approved' NOT NULL,
	"manager_status" text DEFAULT 'pending' NOT NULL,
	"manager_note" text DEFAULT '' NOT NULL,
	"next_due_at" text NOT NULL,
	"notes" text DEFAULT '' NOT NULL,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL,
	CONSTRAINT "inspections_code_unique" UNIQUE("code")
);
--> statement-breakpoint
CREATE TABLE "maintenance" (
	"id" serial PRIMARY KEY NOT NULL,
	"vehicle_id" integer NOT NULL,
	"title" text NOT NULL,
	"due_date" text NOT NULL,
	"due_mileage" integer,
	"cost" integer DEFAULT 0 NOT NULL,
	"vendor" text DEFAULT '' NOT NULL,
	"notes" text DEFAULT '' NOT NULL,
	"status" text DEFAULT 'scheduled' NOT NULL,
	"completed_at" text,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sessions" (
	"token_hash" text PRIMARY KEY NOT NULL,
	"user_id" text NOT NULL,
	"expires_at" text NOT NULL,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sms_logs" (
	"id" serial PRIMARY KEY NOT NULL,
	"event" text NOT NULL,
	"receptor" text NOT NULL,
	"template" text DEFAULT '' NOT NULL,
	"token" text DEFAULT '' NOT NULL,
	"status" text DEFAULT 'unconfigured' NOT NULL,
	"provider_message_id" text,
	"response_message" text DEFAULT '' NOT NULL,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "ticket_messages" (
	"id" serial PRIMARY KEY NOT NULL,
	"ticket_id" integer NOT NULL,
	"author" text NOT NULL,
	"author_role" text DEFAULT 'management' NOT NULL,
	"body" text NOT NULL,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "tickets" (
	"id" serial PRIMARY KEY NOT NULL,
	"code" text NOT NULL,
	"subject" text NOT NULL,
	"category" text DEFAULT 'general' NOT NULL,
	"priority" text DEFAULT 'normal' NOT NULL,
	"status" text DEFAULT 'open' NOT NULL,
	"vehicle_id" integer,
	"driver_id" integer,
	"assignment_id" integer,
	"reporter" text DEFAULT 'مدیر ناوگان' NOT NULL,
	"assignee" text DEFAULT 'سوپروایزر' NOT NULL,
	"description" text DEFAULT '' NOT NULL,
	"closed_at" text,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL,
	"updated_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL,
	CONSTRAINT "tickets_code_unique" UNIQUE("code")
);
--> statement-breakpoint
CREATE TABLE "users" (
	"id" text PRIMARY KEY NOT NULL,
	"username" text NOT NULL,
	"password_hash" text NOT NULL,
	"full_name" text NOT NULL,
	"role" text NOT NULL,
	"driver_id" integer,
	"is_active" boolean DEFAULT true NOT NULL,
	"must_change_password" boolean DEFAULT true NOT NULL,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL,
	CONSTRAINT "users_username_unique" UNIQUE("username")
);
--> statement-breakpoint
CREATE TABLE "vehicle_requests" (
	"id" serial PRIMARY KEY NOT NULL,
	"vehicle_id" integer NOT NULL,
	"driver_id" integer NOT NULL,
	"purpose" text DEFAULT '' NOT NULL,
	"destination" text DEFAULT '' NOT NULL,
	"requested_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL,
	"status" text DEFAULT 'pending' NOT NULL,
	"supervisor_note" text DEFAULT '' NOT NULL,
	"resolved_at" text
);
--> statement-breakpoint
CREATE TABLE "vehicle_types" (
	"id" serial PRIMARY KEY NOT NULL,
	"brand" text NOT NULL,
	"name" text NOT NULL,
	"category" text DEFAULT 'سواری' NOT NULL,
	"image_url" text DEFAULT '' NOT NULL,
	"is_active" boolean DEFAULT true NOT NULL,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "vehicles" (
	"id" serial PRIMARY KEY NOT NULL,
	"type_id" integer,
	"title" text NOT NULL,
	"model" text DEFAULT '' NOT NULL,
	"plate" text NOT NULL,
	"vin" text DEFAULT '' NOT NULL,
	"year" integer,
	"color" text DEFAULT '' NOT NULL,
	"mileage" integer DEFAULT 0 NOT NULL,
	"status" text DEFAULT 'ready' NOT NULL,
	"inspection_interval_days" integer DEFAULT 30 NOT NULL,
	"notes" text DEFAULT '' NOT NULL,
	"created_at" text DEFAULT CURRENT_TIMESTAMP::text NOT NULL,
	CONSTRAINT "vehicles_plate_unique" UNIQUE("plate")
);
--> statement-breakpoint
ALTER TABLE "assignments" ADD CONSTRAINT "assignments_vehicle_id_vehicles_id_fk" FOREIGN KEY ("vehicle_id") REFERENCES "public"."vehicles"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "assignments" ADD CONSTRAINT "assignments_driver_id_drivers_id_fk" FOREIGN KEY ("driver_id") REFERENCES "public"."drivers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inspection_items" ADD CONSTRAINT "inspection_items_inspection_id_inspections_id_fk" FOREIGN KEY ("inspection_id") REFERENCES "public"."inspections"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inspection_photos" ADD CONSTRAINT "inspection_photos_inspection_id_inspections_id_fk" FOREIGN KEY ("inspection_id") REFERENCES "public"."inspections"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inspections" ADD CONSTRAINT "inspections_vehicle_id_vehicles_id_fk" FOREIGN KEY ("vehicle_id") REFERENCES "public"."vehicles"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inspections" ADD CONSTRAINT "inspections_driver_id_drivers_id_fk" FOREIGN KEY ("driver_id") REFERENCES "public"."drivers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inspections" ADD CONSTRAINT "inspections_assignment_id_assignments_id_fk" FOREIGN KEY ("assignment_id") REFERENCES "public"."assignments"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "maintenance" ADD CONSTRAINT "maintenance_vehicle_id_vehicles_id_fk" FOREIGN KEY ("vehicle_id") REFERENCES "public"."vehicles"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ticket_messages" ADD CONSTRAINT "ticket_messages_ticket_id_tickets_id_fk" FOREIGN KEY ("ticket_id") REFERENCES "public"."tickets"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tickets" ADD CONSTRAINT "tickets_vehicle_id_vehicles_id_fk" FOREIGN KEY ("vehicle_id") REFERENCES "public"."vehicles"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tickets" ADD CONSTRAINT "tickets_driver_id_drivers_id_fk" FOREIGN KEY ("driver_id") REFERENCES "public"."drivers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tickets" ADD CONSTRAINT "tickets_assignment_id_assignments_id_fk" FOREIGN KEY ("assignment_id") REFERENCES "public"."assignments"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "users" ADD CONSTRAINT "users_driver_id_drivers_id_fk" FOREIGN KEY ("driver_id") REFERENCES "public"."drivers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vehicle_requests" ADD CONSTRAINT "vehicle_requests_vehicle_id_vehicles_id_fk" FOREIGN KEY ("vehicle_id") REFERENCES "public"."vehicles"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vehicle_requests" ADD CONSTRAINT "vehicle_requests_driver_id_drivers_id_fk" FOREIGN KEY ("driver_id") REFERENCES "public"."drivers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vehicles" ADD CONSTRAINT "vehicles_type_id_vehicle_types_id_fk" FOREIGN KEY ("type_id") REFERENCES "public"."vehicle_types"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_approvals_status" ON "approvals" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_assignments_vehicle_status" ON "assignments" USING btree ("vehicle_id","status");--> statement-breakpoint
CREATE INDEX "idx_assignments_driver_status" ON "assignments" USING btree ("driver_id","status");--> statement-breakpoint
CREATE INDEX "idx_drivers_status" ON "drivers" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_inspection_items_inspection" ON "inspection_items" USING btree ("inspection_id");--> statement-breakpoint
CREATE INDEX "idx_inspection_photos_inspection" ON "inspection_photos" USING btree ("inspection_id");--> statement-breakpoint
CREATE INDEX "idx_inspections_vehicle_date" ON "inspections" USING btree ("vehicle_id","inspected_at");--> statement-breakpoint
CREATE INDEX "idx_inspections_manager_status" ON "inspections" USING btree ("manager_status");--> statement-breakpoint
CREATE INDEX "idx_maintenance_vehicle_status" ON "maintenance" USING btree ("vehicle_id","status");--> statement-breakpoint
CREATE INDEX "idx_maintenance_due_date" ON "maintenance" USING btree ("due_date");--> statement-breakpoint
CREATE INDEX "idx_sessions_user_id" ON "sessions" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "idx_sessions_expires_at" ON "sessions" USING btree ("expires_at");--> statement-breakpoint
CREATE INDEX "idx_sms_logs_status_created" ON "sms_logs" USING btree ("status","created_at");--> statement-breakpoint
CREATE INDEX "idx_ticket_messages_ticket_id" ON "ticket_messages" USING btree ("ticket_id");--> statement-breakpoint
CREATE INDEX "idx_tickets_status_priority" ON "tickets" USING btree ("status","priority");--> statement-breakpoint
CREATE INDEX "idx_tickets_vehicle_id" ON "tickets" USING btree ("vehicle_id");--> statement-breakpoint
CREATE INDEX "idx_users_role_active" ON "users" USING btree ("role","is_active");--> statement-breakpoint
CREATE INDEX "idx_vehicle_requests_driver_status" ON "vehicle_requests" USING btree ("driver_id","status");--> statement-breakpoint
CREATE INDEX "idx_vehicle_requests_vehicle_status" ON "vehicle_requests" USING btree ("vehicle_id","status");--> statement-breakpoint
CREATE INDEX "idx_vehicle_types_active" ON "vehicle_types" USING btree ("is_active");--> statement-breakpoint
CREATE INDEX "idx_vehicles_status" ON "vehicles" USING btree ("status");
