-- Cracker Parameters
-- This migration creates machine-specific parameter tables (must be created before cracking table)

-- =============================================
-- 1. JC CRACKER PARAMETERS TABLE
-- =============================================

CREATE TABLE IF NOT EXISTS public.jc_cracker_parameters (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    plate_contact_frequency_hz DOUBLE PRECISION NOT NULL CHECK (plate_contact_frequency_hz > 0),
    throughput_rate_pecans_sec DOUBLE PRECISION NOT NULL CHECK (throughput_rate_pecans_sec > 0),
    crush_amount_in DOUBLE PRECISION NOT NULL CHECK (crush_amount_in >= 0),
    entry_exit_height_diff_in DOUBLE PRECISION NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- =============================================
-- 2. MEYER CRACKER PARAMETERS TABLE
-- =============================================

CREATE TABLE IF NOT EXISTS public.meyer_cracker_parameters (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    motor_speed_hz DOUBLE PRECISION NOT NULL CHECK (motor_speed_hz > 0),
    jig_displacement_inches DOUBLE PRECISION NOT NULL,
    spring_stiffness_nm DOUBLE PRECISION NOT NULL CHECK (spring_stiffness_nm > 0),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

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

CREATE TRIGGER set_updated_at_jc_cracker_parameters
    BEFORE UPDATE ON public.jc_cracker_parameters
    FOR EACH ROW
    EXECUTE FUNCTION public.handle_updated_at();

CREATE TRIGGER set_updated_at_meyer_cracker_parameters
    BEFORE UPDATE ON public.meyer_cracker_parameters
    FOR EACH ROW
    EXECUTE FUNCTION public.handle_updated_at();

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

GRANT ALL ON public.jc_cracker_parameters TO authenticated;
GRANT ALL ON public.meyer_cracker_parameters TO authenticated;

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

ALTER TABLE public.jc_cracker_parameters ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.meyer_cracker_parameters ENABLE ROW LEVEL SECURITY;

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

CREATE POLICY "JC Cracker parameters are viewable by authenticated users" ON public.jc_cracker_parameters
    FOR SELECT USING (auth.role() = 'authenticated');

CREATE POLICY "JC Cracker parameters are insertable by authenticated users" ON public.jc_cracker_parameters
    FOR INSERT WITH CHECK (auth.role() = 'authenticated');

CREATE POLICY "JC Cracker parameters are updatable by authenticated users" ON public.jc_cracker_parameters
    FOR UPDATE USING (auth.role() = 'authenticated');

CREATE POLICY "JC Cracker parameters are deletable by authenticated users" ON public.jc_cracker_parameters
    FOR DELETE USING (auth.role() = 'authenticated');

CREATE POLICY "Meyer Cracker parameters are viewable by authenticated users" ON public.meyer_cracker_parameters
    FOR SELECT USING (auth.role() = 'authenticated');

CREATE POLICY "Meyer Cracker parameters are insertable by authenticated users" ON public.meyer_cracker_parameters
    FOR INSERT WITH CHECK (auth.role() = 'authenticated');

CREATE POLICY "Meyer Cracker parameters are updatable by authenticated users" ON public.meyer_cracker_parameters
    FOR UPDATE USING (auth.role() = 'authenticated');

CREATE POLICY "Meyer Cracker parameters are deletable by authenticated users" ON public.meyer_cracker_parameters
    FOR DELETE USING (auth.role() = 'authenticated');




