-- Phase 2 JC Experiments Seed Data
-- This file contains all Phase 2 JC experiments from phase_2_experimental_run_sheet.csv
-- Each experiment has 3 repetitions, and each row represents one repetition
-- Updated to use 1-based numbering per phase and composite primary key

-- =============================================
-- INSERT PHASE 2 JC EXPERIMENTS
-- =============================================

-- First, insert unique experiments (based on experiment_number)
INSERT INTO public.experiments (
    experiment_number,
    reps_required,
    results_status,
    completion_status,
    phase_id,
    created_by
) VALUES
-- Phase 2 JC Experiments 1-20 (1-based numbering)
(1, 3, 'valid', false, 
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(2, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(3, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(4, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(5, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(6, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(7, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(8, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(9, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(10, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(11, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(12, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(13, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(14, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(15, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(16, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(17, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(18, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(19, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com')),
(20, 3, 'valid', false,
 (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments'),
 (SELECT id FROM public.user_profiles WHERE email = 's.alireza.v@gmail.com'))
ON CONFLICT (experiment_number, phase_id) DO NOTHING;

-- =============================================
-- CREATE SOAKING PHASE RECORDS FOR PHASE 2 JC EXPERIMENTS
-- =============================================

-- Create soaking records for Phase 2 JC experiments (1-20)
INSERT INTO public.soaking (
    experiment_number,
    experiment_phase_id,
    scheduled_start_time,
    soaking_duration_hours,
    scheduled_end_time,
    created_by
)
SELECT 
    e.experiment_number,
    e.phase_id,
    NOW() + (e.experiment_number) * INTERVAL '1 day',
    CASE e.experiment_number
        WHEN 1 THEN 34  -- hours
        WHEN 2 THEN 24
        WHEN 3 THEN 38
        WHEN 4 THEN 11
        WHEN 5 THEN 13
        WHEN 6 THEN 30
        WHEN 7 THEN 10
        WHEN 8 THEN 15
        WHEN 9 THEN 27
        WHEN 10 THEN 32
        WHEN 11 THEN 26
        WHEN 12 THEN 24
        WHEN 13 THEN 28
        WHEN 14 THEN 21
        WHEN 15 THEN 22
        WHEN 16 THEN 16
        WHEN 17 THEN 20
        WHEN 18 THEN 34
        WHEN 19 THEN 18
        WHEN 20 THEN 11
    END,
    NOW() + (e.experiment_number) * INTERVAL '1 day' + 
    CASE e.experiment_number
        WHEN 1 THEN 34 * INTERVAL '1 hour'
        WHEN 2 THEN 24 * INTERVAL '1 hour'
        WHEN 3 THEN 38 * INTERVAL '1 hour'
        WHEN 4 THEN 11 * INTERVAL '1 hour'
        WHEN 5 THEN 13 * INTERVAL '1 hour'
        WHEN 6 THEN 30 * INTERVAL '1 hour'
        WHEN 7 THEN 10 * INTERVAL '1 hour'
        WHEN 8 THEN 15 * INTERVAL '1 hour'
        WHEN 9 THEN 27 * INTERVAL '1 hour'
        WHEN 10 THEN 32 * INTERVAL '1 hour'
        WHEN 11 THEN 26 * INTERVAL '1 hour'
        WHEN 12 THEN 24 * INTERVAL '1 hour'
        WHEN 13 THEN 28 * INTERVAL '1 hour'
        WHEN 14 THEN 21 * INTERVAL '1 hour'
        WHEN 15 THEN 22 * INTERVAL '1 hour'
        WHEN 16 THEN 16 * INTERVAL '1 hour'
        WHEN 17 THEN 20 * INTERVAL '1 hour'
        WHEN 18 THEN 34 * INTERVAL '1 hour'
        WHEN 19 THEN 18 * INTERVAL '1 hour'
        WHEN 20 THEN 11 * INTERVAL '1 hour'
    END,
    e.created_by
FROM public.experiments e
WHERE e.experiment_number BETWEEN 1 AND 20
AND e.phase_id = (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments')
ON CONFLICT DO NOTHING;

-- =============================================
-- CREATE AIRDRYING PHASE RECORDS FOR PHASE 2 JC EXPERIMENTS
-- =============================================

-- Create airdrying records for Phase 2 JC experiments (1-20)
INSERT INTO public.airdrying (
    experiment_number,
    experiment_phase_id,
    scheduled_start_time,
    duration_minutes,
    scheduled_end_time,
    created_by
)
SELECT 
    e.experiment_number,
    e.phase_id,
    NOW() + (e.experiment_number) * INTERVAL '1 day' + INTERVAL '2 days', -- Start 2 days after soaking
    CASE e.experiment_number
        WHEN 1 THEN 19  -- 19 minutes
        WHEN 2 THEN 27  -- 27 minutes
        WHEN 3 THEN 10  -- 10 minutes
        WHEN 4 THEN 36  -- 36 minutes
        WHEN 5 THEN 41  -- 41 minutes
        WHEN 6 THEN 33  -- 33 minutes
        WHEN 7 THEN 22  -- 22 minutes
        WHEN 8 THEN 30  -- 30 minutes
        WHEN 9 THEN 12  -- 12 minutes
        WHEN 10 THEN 26  -- 26 minutes
        WHEN 11 THEN 60  -- 60 minutes
        WHEN 12 THEN 59  -- 59 minutes
        WHEN 13 THEN 59  -- 59 minutes
        WHEN 14 THEN 59  -- 59 minutes
        WHEN 15 THEN 60  -- 60 minutes
        WHEN 16 THEN 59  -- 59 minutes
        WHEN 17 THEN 60  -- 60 minutes
        WHEN 18 THEN 49  -- 49 minutes
        WHEN 19 THEN 25  -- 25 minutes
        WHEN 20 THEN 25  -- 25 minutes
    END,
    NOW() + (e.experiment_number) * INTERVAL '1 day' + INTERVAL '2 days' + 
    CASE e.experiment_number
        WHEN 1 THEN 19 * INTERVAL '1 minute'  -- 19 minutes
        WHEN 2 THEN 27 * INTERVAL '1 minute'  -- 27 minutes
        WHEN 3 THEN 10 * INTERVAL '1 minute'  -- 10 minutes
        WHEN 4 THEN 36 * INTERVAL '1 minute'  -- 36 minutes
        WHEN 5 THEN 41 * INTERVAL '1 minute'  -- 41 minutes
        WHEN 6 THEN 33 * INTERVAL '1 minute'  -- 33 minutes
        WHEN 7 THEN 22 * INTERVAL '1 minute'  -- 22 minutes
        WHEN 8 THEN 30 * INTERVAL '1 minute'  -- 30 minutes
        WHEN 9 THEN 12 * INTERVAL '1 minute'  -- 12 minutes
        WHEN 10 THEN 26 * INTERVAL '1 minute'  -- 26 minutes
        WHEN 11 THEN 60 * INTERVAL '1 minute'  -- 60 minutes
        WHEN 12 THEN 59 * INTERVAL '1 minute'  -- 59 minutes
        WHEN 13 THEN 59 * INTERVAL '1 minute'  -- 59 minutes
        WHEN 14 THEN 59 * INTERVAL '1 minute'  -- 59 minutes
        WHEN 15 THEN 60 * INTERVAL '1 minute'  -- 60 minutes
        WHEN 16 THEN 59 * INTERVAL '1 minute'  -- 59 minutes
        WHEN 17 THEN 60 * INTERVAL '1 minute'  -- 60 minutes
        WHEN 18 THEN 49 * INTERVAL '1 minute'  -- 49 minutes
        WHEN 19 THEN 25 * INTERVAL '1 minute'  -- 25 minutes
        WHEN 20 THEN 25 * INTERVAL '1 minute'  -- 25 minutes
    END,
    e.created_by
FROM public.experiments e
WHERE e.experiment_number BETWEEN 1 AND 20
AND e.phase_id = (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments')
ON CONFLICT DO NOTHING;

-- =============================================
-- CREATE CRACKING PHASE RECORDS FOR PHASE 2 JC EXPERIMENTS
-- =============================================

-- Create cracking records for Phase 2 JC experiments (1-20)
INSERT INTO public.cracking (
    experiment_number,
    experiment_phase_id,
    machine_type_id,
    scheduled_start_time,
    created_by
)
SELECT 
    e.experiment_number,
    e.phase_id,
    (SELECT id FROM public.machine_types WHERE name = 'JC Cracker'),
    NOW() + (e.experiment_number) * INTERVAL '1 day' + INTERVAL '3 days', -- Start 3 days after soaking
    e.created_by
FROM public.experiments e
WHERE e.experiment_number BETWEEN 1 AND 20
AND e.phase_id = (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments')
ON CONFLICT DO NOTHING;

-- =============================================
-- CREATE JC CRACKER PARAMETERS FOR PHASE 2 JC EXPERIMENTS
-- =============================================

-- Create JC cracker parameters for Phase 2 JC experiments (1-20)
INSERT INTO public.jc_cracker_parameters (
    cracking_id,
    plate_contact_frequency_hz,
    throughput_rate_pecans_sec,
    crush_amount_in,
    entry_exit_height_diff_in
)
SELECT 
    c.id,
    CASE e.experiment_number
        WHEN 1 THEN 53.0
        WHEN 2 THEN 34.0
        WHEN 3 THEN 60.0
        WHEN 4 THEN 42.0
        WHEN 5 THEN 41.0
        WHEN 6 THEN 30.0
        WHEN 7 THEN 37.0
        WHEN 8 THEN 35.0
        WHEN 9 THEN 55.0
        WHEN 10 THEN 47.0
        WHEN 11 THEN 44.0
        WHEN 12 THEN 42.0
        WHEN 13 THEN 37.0
        WHEN 14 THEN 41.0
        WHEN 15 THEN 45.0
        WHEN 16 THEN 30.0
        WHEN 17 THEN 41.0
        WHEN 18 THEN 34.0
        WHEN 19 THEN 38.0
        WHEN 20 THEN 56.0
    END,
    CASE e.experiment_number
        WHEN 1 THEN 28.0
        WHEN 2 THEN 29.0
        WHEN 3 THEN 28.0
        WHEN 4 THEN 13.0
        WHEN 5 THEN 38.0
        WHEN 6 THEN 36.0
        WHEN 7 THEN 30.0
        WHEN 8 THEN 32.0
        WHEN 9 THEN 24.0
        WHEN 10 THEN 26.0
        WHEN 11 THEN 12.0
        WHEN 12 THEN 25.0
        WHEN 13 THEN 23.0
        WHEN 14 THEN 21.0
        WHEN 15 THEN 17.0
        WHEN 16 THEN 24.0
        WHEN 17 THEN 14.0
        WHEN 18 THEN 29.0
        WHEN 19 THEN 35.0
        WHEN 20 THEN 34.0
    END,
    CASE e.experiment_number
        WHEN 1 THEN 0.05
        WHEN 2 THEN 0.03
        WHEN 3 THEN 0.06
        WHEN 4 THEN 0.07
        WHEN 5 THEN 0.05
        WHEN 6 THEN 0.05
        WHEN 7 THEN 0.06
        WHEN 8 THEN 0.05
        WHEN 9 THEN 0.04
        WHEN 10 THEN 0.07
        WHEN 11 THEN 0.08
        WHEN 12 THEN 0.07
        WHEN 13 THEN 0.06
        WHEN 14 THEN 0.06
        WHEN 15 THEN 0.07
        WHEN 16 THEN 0.07
        WHEN 17 THEN 0.07
        WHEN 18 THEN 0.07
        WHEN 19 THEN 0.07
        WHEN 20 THEN 0.06
    END,
    CASE e.experiment_number
        WHEN 1 THEN -0.09
        WHEN 2 THEN 0.01
        WHEN 3 THEN -0.10
        WHEN 4 THEN -0.07
        WHEN 5 THEN 0.03
        WHEN 6 THEN -0.04
        WHEN 7 THEN 0.02
        WHEN 8 THEN -0.07
        WHEN 9 THEN 0.04
        WHEN 10 THEN 0.03
        WHEN 11 THEN -0.10
        WHEN 12 THEN -0.05
        WHEN 13 THEN -0.08
        WHEN 14 THEN -0.09
        WHEN 15 THEN -0.08
        WHEN 16 THEN 0.02
        WHEN 17 THEN 0.04
        WHEN 18 THEN -0.09
        WHEN 19 THEN -0.08
        WHEN 20 THEN -0.09
    END
FROM public.experiments e
JOIN public.cracking c ON c.experiment_number = e.experiment_number AND c.experiment_phase_id = e.phase_id
WHERE e.experiment_number BETWEEN 1 AND 20
AND e.phase_id = (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments')
ON CONFLICT DO NOTHING;

-- =============================================
-- CREATE EXPERIMENT REPETITIONS FOR PHASE 2 JC EXPERIMENTS
-- =============================================

-- Create experiment repetitions for Phase 2 JC experiments
-- Each experiment needs 3 repetitions
INSERT INTO public.experiment_repetitions (
    experiment_number,
    experiment_phase_id,
    repetition_number,
    status,
    created_by
)
SELECT 
    e.experiment_number,
    e.phase_id,
    rep_num,
    'pending',
    e.created_by
FROM public.experiments e
CROSS JOIN generate_series(1, 3) AS rep_num
WHERE e.experiment_number BETWEEN 1 AND 20
AND e.phase_id = (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments')
ON CONFLICT DO NOTHING;
