-- Fix: Convert status column from boolean to company_status enum
-- The previous migration renamed is_active to status but kept the boolean type

-- Step 1: Add a temporary column with the correct enum type
ALTER TABLE "companies" ADD COLUMN "status_new" company_status;

-- Step 2: Migrate data - convert boolean to enum
-- true (active) -> 'registered', false (inactive) -> 'inactive'
UPDATE "companies" SET "status_new" =
  CASE
    WHEN "status"::text = 'true' OR "status"::text = 't' THEN 'registered'::company_status
    WHEN "status"::text = 'false' OR "status"::text = 'f' THEN 'inactive'::company_status
    ELSE 'registered'::company_status
  END;

-- Step 3: Drop the old column
ALTER TABLE "companies" DROP COLUMN "status";

-- Step 4: Rename the new column
ALTER TABLE "companies" RENAME COLUMN "status_new" TO "status";

-- Step 5: Set the default and not null constraint
ALTER TABLE "companies" ALTER COLUMN "status" SET DEFAULT 'registered';
ALTER TABLE "companies" ALTER COLUMN "status" SET NOT NULL;
