-- Hotfix Migration Script
-- Run this manually after accepting drizzle-kit push prompts

-- 1. Create the company_status enum type if it doesn't exist
DO $$ BEGIN
    CREATE TYPE company_status AS ENUM ('registered', 'lead', 'customer', 'inactive');
EXCEPTION
    WHEN duplicate_object THEN null;
END $$;

-- 2. Add the status column to companies table if it doesn't exist
DO $$ BEGIN
    ALTER TABLE companies ADD COLUMN status company_status NOT NULL DEFAULT 'registered';
EXCEPTION
    WHEN duplicate_column THEN null;
END $$;

-- 3. Migrate data from is_active to status (if is_active column exists)
DO $$ BEGIN
    UPDATE companies SET status = CASE
        WHEN is_active = true THEN 'customer'::company_status
        ELSE 'inactive'::company_status
    END;
EXCEPTION
    WHEN undefined_column THEN null;
END $$;

-- 4. Drop the is_active column (optional - run after verifying migration)
-- ALTER TABLE companies DROP COLUMN IF EXISTS is_active;

-- 5. Add description column to personal_contacts if it doesn't exist
DO $$ BEGIN
    ALTER TABLE personal_contacts ADD COLUMN description TEXT;
EXCEPTION
    WHEN duplicate_column THEN null;
END $$;

-- Note: Event type 'important' is stored as text, no schema change needed for events table
