-- Repair domain enums
CREATE TYPE "repair_ticket_status" AS ENUM ('intake', 'diagnosing', 'pending_approval', 'approved', 'in_progress', 'pending_parts', 'ready', 'picked_up', 'delivered', 'cancelled');
CREATE TYPE "repair_line_item_type" AS ENUM ('labor', 'part', 'flat_rate', 'misc');
CREATE TYPE "repair_condition_in" AS ENUM ('excellent', 'good', 'fair', 'poor');
CREATE TYPE "repair_batch_status" AS ENUM ('intake', 'in_progress', 'pending_approval', 'approved', 'completed', 'delivered', 'cancelled');
CREATE TYPE "repair_batch_approval" AS ENUM ('pending', 'approved', 'rejected');

-- Repair batches (defined first — tickets FK to it)
CREATE TABLE "repair_batch" (
  "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  "company_id" uuid NOT NULL REFERENCES "company"("id"),
  "location_id" uuid REFERENCES "location"("id"),
  "batch_number" varchar(50),
  "account_id" uuid NOT NULL REFERENCES "account"("id"),
  "contact_name" varchar(255),
  "contact_phone" varchar(50),
  "contact_email" varchar(255),
  "status" "repair_batch_status" NOT NULL DEFAULT 'intake',
  "approval_status" "repair_batch_approval" NOT NULL DEFAULT 'pending',
  "approved_by" uuid REFERENCES "user"("id"),
  "approved_at" timestamp with time zone,
  "pickup_date" timestamp with time zone,
  "due_date" timestamp with time zone,
  "completed_date" timestamp with time zone,
  "delivered_date" timestamp with time zone,
  "instrument_count" integer NOT NULL DEFAULT 0,
  "received_count" integer NOT NULL DEFAULT 0,
  "estimated_total" numeric(10, 2),
  "actual_total" numeric(10, 2),
  "notes" text,
  "legacy_id" varchar(255),
  "created_at" timestamp with time zone NOT NULL DEFAULT now(),
  "updated_at" timestamp with time zone NOT NULL DEFAULT now()
);

-- Repair tickets
CREATE TABLE "repair_ticket" (
  "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  "company_id" uuid NOT NULL REFERENCES "company"("id"),
  "location_id" uuid REFERENCES "location"("id"),
  "repair_batch_id" uuid REFERENCES "repair_batch"("id"),
  "ticket_number" varchar(50),
  "account_id" uuid REFERENCES "account"("id"),
  "customer_name" varchar(255) NOT NULL,
  "customer_phone" varchar(50),
  "inventory_unit_id" uuid REFERENCES "inventory_unit"("id"),
  "instrument_description" text,
  "serial_number" varchar(255),
  "condition_in" "repair_condition_in",
  "condition_in_notes" text,
  "problem_description" text NOT NULL,
  "technician_notes" text,
  "status" "repair_ticket_status" NOT NULL DEFAULT 'intake',
  "assigned_technician_id" uuid REFERENCES "user"("id"),
  "estimated_cost" numeric(10, 2),
  "actual_cost" numeric(10, 2),
  "intake_date" timestamp with time zone NOT NULL DEFAULT now(),
  "promised_date" timestamp with time zone,
  "completed_date" timestamp with time zone,
  "legacy_id" varchar(255),
  "created_at" timestamp with time zone NOT NULL DEFAULT now(),
  "updated_at" timestamp with time zone NOT NULL DEFAULT now()
);

-- Repair line items
CREATE TABLE "repair_line_item" (
  "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
  "repair_ticket_id" uuid NOT NULL REFERENCES "repair_ticket"("id"),
  "item_type" "repair_line_item_type" NOT NULL,
  "description" varchar(255) NOT NULL,
  "product_id" uuid REFERENCES "product"("id"),
  "qty" numeric(10, 3) NOT NULL DEFAULT 1,
  "unit_price" numeric(10, 2) NOT NULL DEFAULT 0,
  "total_price" numeric(10, 2) NOT NULL DEFAULT 0,
  "cost" numeric(10, 2),
  "technician_id" uuid REFERENCES "user"("id"),
  "created_at" timestamp with time zone NOT NULL DEFAULT now()
);
