-- Baltique OS Core 0.1 reference data
INSERT INTO roles (slug, name, description, permissions_json) VALUES
('superadmin','Superadmin','Pełny dostęp techniczny i organizacyjny','{"all":true}'),
('admin','Administrator','Administracja systemem, użytkownikami i konfiguracją','{"admin":true}'),
('reception','Recepcja','Rezerwacje, pacjenci, płatności manualne','{"booking":true,"patients":true}'),
('coordinator','Koordynator pacjenta','Paszport Opieki, zadania, komunikacja','{"patients":true,"tasks":true}'),
('surgeon','Chirurg','Widok pacjentów, konsultacji i przygotowania','{"clinical_view":true}'),
('anesthesiologist','Anestezjolog','Widok przygotowania anestezjologicznego','{"clinical_view":true}'),
('nurse','Pielęgniarka','Zadania pacjenta i komunikacja opieki','{"care_tasks":true}'),
('block','Blok operacyjny','Planowanie przyszłego TheatreFlow','{"block":true}'),
('ward','Oddział','Planowanie przyszłego WardView','{"ward":true}'),
('supply','Magazyn','Przyszły Supply','{"supply":true}'),
('management','Dyrekcja','Raporty i mapa dowodzenia','{"reports":true}'),
('patient','Pacjent','Portal pacjenta i Paszport Opieki','{"patient_portal":true}')
ON DUPLICATE KEY UPDATE name = VALUES(name), description = VALUES(description), permissions_json = VALUES(permissions_json);

INSERT INTO locations (slug, name, address_line, city, postal_code, kind, status) VALUES
('sopot-obecny','Baltique Sopot — obecny gabinet','al. Niepodległości 761 lok. 1.3','Sopot','81-838','clinic','current'),
('sopot-poniatowskiego','Baltique Sopot — przyszła lokalizacja','ul. Poniatowskiego 7','Sopot','81-724','clinic','future'),
('gdansk-hallera','Baltique Gdańsk','al. Hallera 223 lok. 1','Gdańsk','80-502','clinic','future'),
('online','Konsultacja online','Portal pacjenta Baltique','Online','','online','active')
ON DUPLICATE KEY UPDATE name = VALUES(name), address_line = VALUES(address_line), city = VALUES(city), postal_code = VALUES(postal_code), kind = VALUES(kind), status = VALUES(status);

INSERT INTO providers (display_name, title, specialty, public_bookable, active) VALUES
('prof. Paweł Szychta','prof.','chirurgia plastyczna',1,1),
('mgr Sabina Imińska','mgr','fizjoterapia',0,1),
('dr Anna Nowicka','dr','medycyna estetyczna',0,1),
('mgr Anna Kowalska','mgr','fizjoterapia',0,1)
ON DUPLICATE KEY UPDATE display_name = VALUES(display_name), specialty = VALUES(specialty), public_bookable = VALUES(public_bookable), active = VALUES(active);

INSERT INTO consultation_types (slug, name, duration_minutes, price_gross_cents, public_bookable, requires_payment, active) VALUES
('first_plastic_surgery','Konsultacja pierwszorazowa',30,60000,1,1,1),
('followup_plastic_surgery','Konsultacja kolejna',30,60000,1,1,1),
('consultation_3d','Konsultacja 3D',30,100000,1,1,1),
('online','Konsultacja online',30,60000,1,1,1),
('online_3d','Konsultacja online 3D',30,100000,1,1,1),
('early_control','Wizyta kontrolna wczesna',15,0,0,0,1),
('remote_control','Wizyta kontrolna odległa',15,0,0,0,1),
('additional_control','Wizyta kontrolna dodatkowa',15,30000,0,1,1),
('physio_consultation','Konsultacja fizjoterapeutyczna',30,27000,0,1,1),
('aesthetic_consultation','Konsultacja medycyny estetycznej',30,40000,0,1,1),
('aesthetic_procedure','Zabieg medycyny estetycznej',30,0,0,0,1)
ON DUPLICATE KEY UPDATE name = VALUES(name), duration_minutes = VALUES(duration_minutes), price_gross_cents = VALUES(price_gross_cents), public_bookable = VALUES(public_bookable), requires_payment = VALUES(requires_payment), active = VALUES(active);

-- Provider-location mapping by subqueries to keep seed idempotent
-- Prof. Paweł Szychta: Sopot obecny, Sopot przyszły, Gdańsk, online.
INSERT IGNORE INTO provider_locations (provider_id, location_id)
SELECT p.id, l.id FROM providers p JOIN locations l WHERE p.display_name = 'prof. Paweł Szychta' AND l.slug IN ('sopot-obecny','sopot-poniatowskiego','gdansk-hallera','online');
-- Fizjoterapia i medycyna estetyczna: Sopot obecny, Sopot przyszły, Gdańsk.
INSERT IGNORE INTO provider_locations (provider_id, location_id)
SELECT p.id, l.id FROM providers p JOIN locations l WHERE p.display_name IN ('mgr Sabina Imińska','dr Anna Nowicka','mgr Anna Kowalska') AND l.slug IN ('sopot-obecny','sopot-poniatowskiego','gdansk-hallera');

-- Prof. Szychta: all public types except controls. Controls are staff-only.
INSERT IGNORE INTO provider_consultation_types (provider_id, consultation_type_id, staff_only)
SELECT p.id, ct.id, 0 FROM providers p JOIN consultation_types ct WHERE p.display_name='prof. Paweł Szychta' AND ct.slug IN ('first_plastic_surgery','followup_plastic_surgery','consultation_3d','online','online_3d');
INSERT IGNORE INTO provider_consultation_types (provider_id, consultation_type_id, staff_only)
SELECT p.id, ct.id, 1 FROM providers p JOIN consultation_types ct WHERE p.display_name='prof. Paweł Szychta' AND ct.slug IN ('early_control','remote_control','additional_control');

INSERT IGNORE INTO provider_consultation_types (provider_id, consultation_type_id, staff_only)
SELECT p.id, ct.id, 1 FROM providers p JOIN consultation_types ct WHERE p.display_name IN ('mgr Sabina Imińska','mgr Anna Kowalska') AND ct.slug='physio_consultation';
INSERT IGNORE INTO provider_consultation_types (provider_id, consultation_type_id, staff_only)
SELECT p.id, ct.id, 1 FROM providers p JOIN consultation_types ct WHERE p.display_name='dr Anna Nowicka' AND ct.slug IN ('aesthetic_consultation','aesthetic_procedure');

-- Schedules: weekday 1=Monday ... 7=Sunday
INSERT INTO schedules (provider_id, location_id, weekday, starts_at, ends_at, slot_minutes, bookable_online, notes, active)
SELECT p.id, NULL, 5, '09:00:00', '19:00:00', 30, 1, 'Piątkowe konsultacje chirurgii plastycznej', 1 FROM providers p WHERE p.display_name='prof. Paweł Szychta'
ON DUPLICATE KEY UPDATE active=VALUES(active);
INSERT INTO schedules (provider_id, location_id, weekday, starts_at, ends_at, slot_minutes, bookable_online, notes, active)
SELECT p.id, NULL, 1, '09:00:00', '15:00:00', 15, 0, 'Poniedziałkowe kontrole', 1 FROM providers p WHERE p.display_name='prof. Paweł Szychta'
ON DUPLICATE KEY UPDATE active=VALUES(active);
INSERT INTO schedules (provider_id, location_id, weekday, starts_at, ends_at, slot_minutes, bookable_online, notes, active)
SELECT p.id, NULL, 2, '10:00:00', '15:00:00', 30, 0, 'Fizjoterapia wtorek', 1 FROM providers p WHERE p.display_name='mgr Sabina Imińska'
ON DUPLICATE KEY UPDATE active=VALUES(active);
INSERT INTO schedules (provider_id, location_id, weekday, starts_at, ends_at, slot_minutes, bookable_online, notes, active)
SELECT p.id, NULL, 4, '10:00:00', '15:00:00', 30, 0, 'Fizjoterapia czwartek', 1 FROM providers p WHERE p.display_name='mgr Sabina Imińska'
ON DUPLICATE KEY UPDATE active=VALUES(active);
INSERT INTO schedules (provider_id, location_id, weekday, starts_at, ends_at, slot_minutes, bookable_online, notes, active)
SELECT p.id, NULL, 2, '09:00:00', '15:00:00', 30, 0, 'Medycyna estetyczna wtorek', 1 FROM providers p WHERE p.display_name='dr Anna Nowicka'
ON DUPLICATE KEY UPDATE active=VALUES(active);
INSERT INTO schedules (provider_id, location_id, weekday, starts_at, ends_at, slot_minutes, bookable_online, notes, active)
SELECT p.id, NULL, 4, '09:00:00', '15:00:00', 30, 0, 'Medycyna estetyczna czwartek', 1 FROM providers p WHERE p.display_name='dr Anna Nowicka'
ON DUPLICATE KEY UPDATE active=VALUES(active);
INSERT INTO schedules (provider_id, location_id, weekday, starts_at, ends_at, slot_minutes, bookable_online, notes, active)
SELECT p.id, NULL, 3, '10:00:00', '15:00:00', 30, 0, 'Fizjoterapia środa', 1 FROM providers p WHERE p.display_name='mgr Anna Kowalska'
ON DUPLICATE KEY UPDATE active=VALUES(active);

INSERT INTO app_settings (setting_key, setting_value) VALUES
('booking.slot_hold_minutes','30'),
('booking.patient_self_reschedule_days','7'),
('booking.assisted_reschedule_min_hours','48'),
('surgery.deposit_percent','10'),
('surgery.balance_due_days_before','14'),
('system.scope','OpenCare formal medical system; Baltique OS operational patient and clinic OS')
ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value), updated_at = NOW();
