CREATE TABLE "forklift_companies" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" text NOT NULL,
	CONSTRAINT "forklift_companies_name_unique" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE "leases" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"lease_number" text NOT NULL,
	"company_id" uuid,
	"start_date" date,
	"end_date" date,
	"lease_link" text
);
--> statement-breakpoint
CREATE TABLE "lease_invoice_forklifts" (
	"id" serial PRIMARY KEY NOT NULL,
	"invoice_id" uuid NOT NULL,
	"forklift_id" uuid NOT NULL,
	"amount" numeric
);
--> statement-breakpoint
CREATE TABLE "forklifts" (
	"forklift_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"forklift_number" serial NOT NULL,
	"serial_number" text NOT NULL,
	"model" text NOT NULL,
	"plant" text NOT NULL,
	"forklift_status" "forklift_status" DEFAULT 'active',
	"gl_code" integer NOT NULL,
	"profit_center" integer NOT NULL,
	"manufacturer" text NOT NULL,
	"manufacturer_year" text NOT NULL,
	"engine" text NOT NULL,
	"battery_type" text NOT NULL,
	"lease_id" uuid,
	"data_plate" text,
	"add_date" timestamp DEFAULT now(),
	"add_user" text DEFAULT 'LST'
);
--> statement-breakpoint
CREATE TABLE "lease_invoices" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"lease_id" uuid NOT NULL,
	"company_id" uuid,
	"invoice_number" text NOT NULL,
	"invoice_date" date NOT NULL,
	"forklift_id" uuid NOT NULL,
	"total_amount" numeric,
	"uploaded_by" text
);
--> statement-breakpoint
ALTER TABLE "leases" ADD CONSTRAINT "leases_company_id_forklift_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."forklift_companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lease_invoice_forklifts" ADD CONSTRAINT "lease_invoice_forklifts_invoice_id_lease_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."lease_invoices"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lease_invoice_forklifts" ADD CONSTRAINT "lease_invoice_forklifts_forklift_id_forklifts_forklift_id_fk" FOREIGN KEY ("forklift_id") REFERENCES "public"."forklifts"("forklift_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "forklifts" ADD CONSTRAINT "forklifts_plant_serverData_name_fk" FOREIGN KEY ("plant") REFERENCES "public"."serverData"("name") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "forklifts" ADD CONSTRAINT "forklifts_lease_id_leases_id_fk" FOREIGN KEY ("lease_id") REFERENCES "public"."leases"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lease_invoices" ADD CONSTRAINT "lease_invoices_lease_id_leases_id_fk" FOREIGN KEY ("lease_id") REFERENCES "public"."leases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lease_invoices" ADD CONSTRAINT "lease_invoices_company_id_forklift_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."forklift_companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "lease_invoices" ADD CONSTRAINT "lease_invoices_forklift_id_forklifts_forklift_id_fk" FOREIGN KEY ("forklift_id") REFERENCES "public"."forklifts"("forklift_id") ON DELETE cascade ON UPDATE no action;