-- Phase 2 JC Experiments Seed Data (Corrected)
-- This file contains all Phase 2 JC experiments from phase_2_JC_experimental_run_sheet.csv
-- Each experiment has 3 repetitions, and each row represents one repetition
-- Updated to match the actual CSV data with correct experiment numbers and parameters

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

-- First, insert unique experiments (based on experiment_number from CSV: 0-19)
INSERT INTO public.experiments (
    experiment_number,
    reps_required,
    results_status,
    completion_status,
    phase_id,
    created_by
) VALUES
-- Phase 2 JC Experiments 0-19 (matching CSV data)
(0, 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')),
(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'))
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 (0-19) with data from CSV
INSERT INTO public.soaking (
    experiment_id,
    scheduled_start_time,
    soaking_duration_minutes,
    scheduled_end_time,
    created_by
)
SELECT 
    e.id,
    NOW() + (e.experiment_number + 1) * INTERVAL '1 day',
    CASE e.experiment_number
        WHEN 0 THEN 34 * 60  -- 34 hours = 2040 minutes
        WHEN 1 THEN 24 * 60  -- 24 hours = 1440 minutes
        WHEN 2 THEN 38 * 60  -- 38 hours = 2280 minutes
        WHEN 3 THEN 11 * 60  -- 11 hours = 660 minutes
        WHEN 4 THEN 13 * 60  -- 13 hours = 780 minutes
        WHEN 5 THEN 30 * 60  -- 30 hours = 1800 minutes
        WHEN 6 THEN 10 * 60  -- 10 hours = 600 minutes
        WHEN 7 THEN 15 * 60  -- 15 hours = 900 minutes
        WHEN 8 THEN 27 * 60  -- 27 hours = 1620 minutes
        WHEN 9 THEN 32 * 60  -- 32 hours = 1920 minutes
        WHEN 10 THEN 26 * 60  -- 26 hours = 1560 minutes
        WHEN 11 THEN 24 * 60  -- 24 hours = 1440 minutes
        WHEN 12 THEN 28 * 60  -- 28 hours = 1680 minutes
        WHEN 13 THEN 21 * 60  -- 21 hours = 1260 minutes
        WHEN 14 THEN 22 * 60  -- 22 hours = 1320 minutes
        WHEN 15 THEN 16 * 60  -- 16 hours = 960 minutes
        WHEN 16 THEN 20 * 60  -- 20 hours = 1200 minutes
        WHEN 17 THEN 34 * 60  -- 34 hours = 2040 minutes
        WHEN 18 THEN 18 * 60  -- 18 hours = 1080 minutes
        WHEN 19 THEN 11 * 60  -- 11 hours = 660 minutes
    END,
    NOW() + (e.experiment_number + 1) * INTERVAL '1 day' + 
    CASE e.experiment_number
        WHEN 0 THEN 34 * 60 * INTERVAL '1 minute'  -- 34 hours = 2040 minutes
        WHEN 1 THEN 24 * 60 * INTERVAL '1 minute'  -- 24 hours = 1440 minutes
        WHEN 2 THEN 38 * 60 * INTERVAL '1 minute'  -- 38 hours = 2280 minutes
        WHEN 3 THEN 11 * 60 * INTERVAL '1 minute'  -- 11 hours = 660 minutes
        WHEN 4 THEN 13 * 60 * INTERVAL '1 minute'  -- 13 hours = 780 minutes
        WHEN 5 THEN 30 * 60 * INTERVAL '1 minute'  -- 30 hours = 1800 minutes
        WHEN 6 THEN 10 * 60 * INTERVAL '1 minute'  -- 10 hours = 600 minutes
        WHEN 7 THEN 15 * 60 * INTERVAL '1 minute'  -- 15 hours = 900 minutes
        WHEN 8 THEN 27 * 60 * INTERVAL '1 minute'  -- 27 hours = 1620 minutes
        WHEN 9 THEN 32 * 60 * INTERVAL '1 minute'  -- 32 hours = 1920 minutes
        WHEN 10 THEN 26 * 60 * INTERVAL '1 minute'  -- 26 hours = 1560 minutes
        WHEN 11 THEN 24 * 60 * INTERVAL '1 minute'  -- 24 hours = 1440 minutes
        WHEN 12 THEN 28 * 60 * INTERVAL '1 minute'  -- 28 hours = 1680 minutes
        WHEN 13 THEN 21 * 60 * INTERVAL '1 minute'  -- 21 hours = 1260 minutes
        WHEN 14 THEN 22 * 60 * INTERVAL '1 minute'  -- 22 hours = 1320 minutes
        WHEN 15 THEN 16 * 60 * INTERVAL '1 minute'  -- 16 hours = 960 minutes
        WHEN 16 THEN 20 * 60 * INTERVAL '1 minute'  -- 20 hours = 1200 minutes
        WHEN 17 THEN 34 * 60 * INTERVAL '1 minute'  -- 34 hours = 2040 minutes
        WHEN 18 THEN 18 * 60 * INTERVAL '1 minute'  -- 18 hours = 1080 minutes
        WHEN 19 THEN 11 * 60 * INTERVAL '1 minute'  -- 11 hours = 660 minutes
    END,
    e.created_by
FROM public.experiments e
WHERE e.experiment_number BETWEEN 0 AND 19
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 (0-19) with data from CSV
INSERT INTO public.airdrying (
    experiment_id,
    scheduled_start_time,
    duration_minutes,
    scheduled_end_time,
    created_by
)
SELECT 
    e.id,
    NOW() + (e.experiment_number + 1) * INTERVAL '1 day' + INTERVAL '2 days', -- Start 2 days after soaking
    CASE e.experiment_number
        WHEN 0 THEN 19  -- 19 minutes
        WHEN 1 THEN 27  -- 27 minutes
        WHEN 2 THEN 10  -- 10 minutes
        WHEN 3 THEN 36  -- 36 minutes
        WHEN 4 THEN 41  -- 41 minutes
        WHEN 5 THEN 33  -- 33 minutes
        WHEN 6 THEN 22  -- 22 minutes
        WHEN 7 THEN 30  -- 30 minutes
        WHEN 8 THEN 12  -- 12 minutes
        WHEN 9 THEN 26  -- 26 minutes
        WHEN 10 THEN 60  -- 60 minutes
        WHEN 11 THEN 59  -- 59 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
    END,
    NOW() + (e.experiment_number + 1) * INTERVAL '1 day' + INTERVAL '2 days' + 
    CASE e.experiment_number
        WHEN 0 THEN 19 * INTERVAL '1 minute'  -- 19 minutes
        WHEN 1 THEN 27 * INTERVAL '1 minute'  -- 27 minutes
        WHEN 2 THEN 10 * INTERVAL '1 minute'  -- 10 minutes
        WHEN 3 THEN 36 * INTERVAL '1 minute'  -- 36 minutes
        WHEN 4 THEN 41 * INTERVAL '1 minute'  -- 41 minutes
        WHEN 5 THEN 33 * INTERVAL '1 minute'  -- 33 minutes
        WHEN 6 THEN 22 * INTERVAL '1 minute'  -- 22 minutes
        WHEN 7 THEN 30 * INTERVAL '1 minute'  -- 30 minutes
        WHEN 8 THEN 12 * INTERVAL '1 minute'  -- 12 minutes
        WHEN 9 THEN 26 * INTERVAL '1 minute'  -- 26 minutes
        WHEN 10 THEN 60 * INTERVAL '1 minute'  -- 60 minutes
        WHEN 11 THEN 59 * INTERVAL '1 minute'  -- 59 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
    END,
    e.created_by
FROM public.experiments e
WHERE e.experiment_number BETWEEN 0 AND 19
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 (0-19)
INSERT INTO public.cracking (
    experiment_id,
    machine_type_id,
    scheduled_start_time,
    created_by
)
SELECT 
    e.id,
    (SELECT id FROM public.machine_types WHERE name = 'JC Cracker'),
    NOW() + (e.experiment_number + 1) * INTERVAL '1 day' + INTERVAL '3 days', -- Start 3 days after soaking
    e.created_by
FROM public.experiments e
WHERE e.experiment_number BETWEEN 0 AND 19
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 (0-19) with data from CSV
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 0 THEN 53.0
        WHEN 1 THEN 34.0
        WHEN 2 THEN 60.0
        WHEN 3 THEN 42.0
        WHEN 4 THEN 41.0
        WHEN 5 THEN 30.0
        WHEN 6 THEN 37.0
        WHEN 7 THEN 35.0
        WHEN 8 THEN 55.0
        WHEN 9 THEN 47.0
        WHEN 10 THEN 44.0
        WHEN 11 THEN 42.0
        WHEN 12 THEN 37.0
        WHEN 13 THEN 41.0
        WHEN 14 THEN 45.0
        WHEN 15 THEN 30.0
        WHEN 16 THEN 41.0
        WHEN 17 THEN 34.0
        WHEN 18 THEN 38.0
        WHEN 19 THEN 56.0
    END,
    CASE e.experiment_number
        WHEN 0 THEN 28.0
        WHEN 1 THEN 29.0
        WHEN 2 THEN 28.0
        WHEN 3 THEN 13.0
        WHEN 4 THEN 38.0
        WHEN 5 THEN 36.0
        WHEN 6 THEN 30.0
        WHEN 7 THEN 32.0
        WHEN 8 THEN 24.0
        WHEN 9 THEN 26.0
        WHEN 10 THEN 12.0
        WHEN 11 THEN 25.0
        WHEN 12 THEN 23.0
        WHEN 13 THEN 21.0
        WHEN 14 THEN 17.0
        WHEN 15 THEN 24.0
        WHEN 16 THEN 14.0
        WHEN 17 THEN 29.0
        WHEN 18 THEN 35.0
        WHEN 19 THEN 34.0
    END,
    CASE e.experiment_number
        WHEN 0 THEN 0.05
        WHEN 1 THEN 0.03
        WHEN 2 THEN 0.06
        WHEN 3 THEN 0.07
        WHEN 4 THEN 0.05
        WHEN 5 THEN 0.05
        WHEN 6 THEN 0.06
        WHEN 7 THEN 0.05
        WHEN 8 THEN 0.04
        WHEN 9 THEN 0.07
        WHEN 10 THEN 0.08
        WHEN 11 THEN 0.07
        WHEN 12 THEN 0.06
        WHEN 13 THEN 0.06
        WHEN 14 THEN 0.07
        WHEN 15 THEN 0.07
        WHEN 16 THEN 0.07
        WHEN 17 THEN 0.07
        WHEN 18 THEN 0.07
        WHEN 19 THEN 0.06
    END,
    CASE e.experiment_number
        WHEN 0 THEN -0.09
        WHEN 1 THEN 0.01
        WHEN 2 THEN -0.10
        WHEN 3 THEN -0.07
        WHEN 4 THEN 0.03
        WHEN 5 THEN -0.04
        WHEN 6 THEN 0.02
        WHEN 7 THEN -0.07
        WHEN 8 THEN 0.04
        WHEN 9 THEN 0.03
        WHEN 10 THEN -0.10
        WHEN 11 THEN -0.05
        WHEN 12 THEN -0.08
        WHEN 13 THEN -0.09
        WHEN 14 THEN -0.08
        WHEN 15 THEN 0.02
        WHEN 16 THEN 0.04
        WHEN 17 THEN -0.09
        WHEN 18 THEN -0.08
        WHEN 19 THEN -0.09
    END
FROM public.experiments e
JOIN public.cracking c ON c.experiment_id = e.id
WHERE e.experiment_number BETWEEN 0 AND 19
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_id,
    repetition_number,
    completion_status,
    created_by
)
SELECT 
    e.id,
    rep_num,
    false,
    e.created_by
FROM public.experiments e
CROSS JOIN generate_series(1, 3) AS rep_num
WHERE e.experiment_number BETWEEN 0 AND 19
AND e.phase_id = (SELECT id FROM public.experiment_phases WHERE name = 'Phase 2 of JC Experiments')
ON CONFLICT DO NOTHING;
