-- Migration: Ensure jobs.work_mode exists and accepts frontend values

ALTER TABLE jobs
    ADD COLUMN IF NOT EXISTS work_mode VARCHAR(20);

-- Normalize legacy/invalid values before enforcing constraint
UPDATE jobs
SET work_mode = 'onsite'
WHERE work_mode IS NULL
   OR work_mode NOT IN ('onsite', 'hybrid', 'remote');

ALTER TABLE jobs
    ALTER COLUMN work_mode SET DEFAULT 'onsite';

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'jobs_work_mode_check'
    ) THEN
        ALTER TABLE jobs
            ADD CONSTRAINT jobs_work_mode_check
            CHECK (work_mode IN ('onsite', 'hybrid', 'remote'));
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_jobs_work_mode ON jobs(work_mode);
