INSERT INTO machines (code, name, brand, model, status)
VALUES
    ('INJ-01', 'INJ-01', 'Haitian', 'Mars II', 'production'),
    ('INJ-02', 'INJ-02', 'Engel', 'e-victory', 'hors_planning'),
    ('INJ-03', 'INJ-03', 'Arburg', 'Allrounder', 'hors_planning')
ON CONFLICT (code) DO NOTHING;

INSERT INTO downtime_reasons (code, label)
VALUES
    ('changement_moule', 'Changement moule'),
    ('reglage', 'Reglage'),
    ('attente_matiere', 'Attente matiere'),
    ('attente_operateur', 'Attente operateur'),
    ('panne_machine', 'Panne machine'),
    ('panne_moule', 'Panne moule'),
    ('attente_qualite', 'Attente qualite'),
    ('nettoyage', 'Nettoyage'),
    ('pause', 'Pause'),
    ('micro_arret', 'Micro arret'),
    ('autre', 'Autre')
ON CONFLICT (code) DO NOTHING;

INSERT INTO scrap_reasons (code, label)
VALUES
    ('bavure', 'Bavure'),
    ('manque_matiere', 'Manque matiere'),
    ('brulure', 'Brulure'),
    ('retassure', 'Retassure'),
    ('deformation', 'Deformation'),
    ('point_noir', 'Point noir'),
    ('humidite', 'Humidite'),
    ('couleur_non_conforme', 'Couleur non conforme'),
    ('collage_moule', 'Collage moule'),
    ('casse_piece', 'Casse piece'),
    ('defaut_dimensionnel', 'Defaut dimensionnel'),
    ('autre', 'Autre')
ON CONFLICT (code) DO NOTHING;

INSERT INTO operators (name)
VALUES
    ('operateur_1'),
    ('operateur_2')
ON CONFLICT (name) DO NOTHING;

INSERT INTO production_orders (
    order_number,
    machine_id,
    article_ref,
    article_name,
    mold_ref,
    material_ref,
    planned_qty,
    cavities,
    theoretical_cycle_time_sec,
    status,
    started_at
)
SELECT
    'OF-1001',
    m.id,
    'ART-001',
    'Boitier 1L',
    'M-001',
    'PP-NEUTRAL',
    4000,
    2,
    18.5,
    'running',
    NOW()
FROM machines m
WHERE m.code = 'INJ-01'
ON CONFLICT (order_number) DO NOTHING;

INSERT INTO production_orders (
    order_number,
    machine_id,
    article_ref,
    article_name,
    mold_ref,
    material_ref,
    planned_qty,
    cavities,
    theoretical_cycle_time_sec,
    status,
    started_at
)
SELECT
    'OF-1002',
    m.id,
    'ART-002',
    'Capot Technique',
    'M-002',
    'ABS-BLACK',
    2500,
    1,
    24.0,
    'running',
    NOW()
FROM machines m
WHERE m.code = 'INJ-02'
ON CONFLICT (order_number) DO NOTHING;
