-- Experiment Phases
-- This migration creates the experiment phases table

-- =============================================
-- 1. EXPERIMENT PHASES TABLE
-- =============================================

CREATE TABLE IF NOT EXISTS public.experiment_phases (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    name TEXT NOT NULL UNIQUE,
    description TEXT,
    has_soaking BOOLEAN NOT NULL DEFAULT false,
    has_airdrying BOOLEAN NOT NULL DEFAULT false,
    has_cracking BOOLEAN NOT NULL DEFAULT false,
    has_shelling BOOLEAN NOT NULL DEFAULT false,
    cracking_machine_type_id UUID REFERENCES public.machine_types(id) ON DELETE SET NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    created_by UUID NOT NULL REFERENCES public.user_profiles(id),
    
    -- Ensure at least one phase is selected
    CONSTRAINT check_at_least_one_phase 
    CHECK (has_soaking = true OR has_airdrying = true OR has_cracking = true OR has_shelling = true),
    
    -- If has_cracking is true, then cracking_machine_type_id must not be null
    CONSTRAINT ck_experiment_phases_machine_required_when_cracking
    CHECK ((has_cracking = false) OR (cracking_machine_type_id IS NOT NULL))
);

-- =============================================
-- 2. INDEXES FOR PERFORMANCE
-- =============================================

CREATE INDEX IF NOT EXISTS idx_experiment_phases_name ON public.experiment_phases(name);
CREATE INDEX IF NOT EXISTS idx_experiment_phases_cracking_machine_type_id ON public.experiment_phases(cracking_machine_type_id);

-- =============================================
-- 3. TRIGGERS
-- =============================================

-- Create trigger for updated_at on experiment_phases
CREATE TRIGGER set_updated_at_experiment_phases
    BEFORE UPDATE ON public.experiment_phases
    FOR EACH ROW
    EXECUTE FUNCTION public.handle_updated_at();

-- =============================================
-- 4. GRANT PERMISSIONS
-- =============================================

GRANT ALL ON public.experiment_phases TO authenticated;

-- =============================================
-- 5. ENABLE ROW LEVEL SECURITY
-- =============================================

ALTER TABLE public.experiment_phases ENABLE ROW LEVEL SECURITY;

-- =============================================
-- 6. CREATE RLS POLICIES
-- =============================================

CREATE POLICY "Experiment phases are viewable by authenticated users" ON public.experiment_phases
    FOR SELECT USING (auth.role() = 'authenticated');

CREATE POLICY "Experiment phases are insertable by authenticated users" ON public.experiment_phases
    FOR INSERT WITH CHECK (auth.role() = 'authenticated');

CREATE POLICY "Experiment phases are updatable by authenticated users" ON public.experiment_phases
    FOR UPDATE USING (auth.role() = 'authenticated');

CREATE POLICY "Experiment phases are deletable by authenticated users" ON public.experiment_phases
    FOR DELETE USING (auth.role() = 'authenticated');



