-- ============================================================
-- TumbuhSehat v3 - Seed data: clinics, clinic scoping, app versions
-- Run this AFTER db/schema_v3.sql (and after schema_v2.sql + seed_v2.sql)
-- ============================================================

USE `tumbuhsehat`;

SET FOREIGN_KEY_CHECKS = 0;

-- ---------- 3 sample clinics ----------
INSERT INTO `clinics` (`id`,`name`,`slug`,`address`,`phone`,`logo_url`,`is_active`) VALUES
(1,'RS Utama Jakarta','rs-utama-jakarta','Jl. Jenderal Sudirman No. 1, Jakarta Pusat, DKI Jakarta','021-5000101','https://placehold.co/120x120/0F766E/FFFFFF?text=RSU',1),
(2,'Klinik Sehat Tumbuh Bandung','klinik-sehat-tumbuh-bandung','Jl. Ir. H. Juanda No. 220, Bandung, Jawa Barat','022-7200202','https://placehold.co/120x120/2563EB/FFFFFF?text=KST',1),
(3,'Praktik Mandiri Terapi Surabaya','praktik-mandiri-terapi-surabaya','Jl. Prof. Dr. Moestopo No. 45, Surabaya, Jawa Timur','031-5600303','https://placehold.co/120x120/7C3AED/FFFFFF?text=PMT',1)
ON DUPLICATE KEY UPDATE `name` = VALUES(`name`), `address` = VALUES(`address`), `phone` = VALUES(`phone`);

-- ---------- promote the platform admin to super_admin ----------
UPDATE `users` SET `role` = 'super_admin', `clinic_id` = NULL WHERE `id` = 99;
UPDATE `users` SET `role` = 'super_admin', `clinic_id` = NULL WHERE `email` = 'admin@tumbuhsehat.id';

-- ---------- add a clinic_admin (admin of RS Utama Jakarta) ----------
INSERT INTO `users` (`id`,`name`,`email`,`password_hash`,`role`,`phone`,`clinic_id`,`specialty`)
VALUES (100,'Admin RS Utama Jakarta','klinik@tumbuhsehat.id',
'bee5688aea66a47460b19c76f8f199c6b9585eb726f8322b1429793863609ca2','clinic_admin','628999000002',1,NULL)
ON DUPLICATE KEY UPDATE `role` = 'clinic_admin', `clinic_id` = 1;

-- ---------- assign existing users to clinics ----------
-- doctors / therapists / parents default to RS Utama Jakarta (clinic 1)
UPDATE `users` SET `clinic_id` = 1 WHERE `role` IN ('doctor','therapist','parent') AND `clinic_id` IS NULL;

-- move a slice of the team to the Bandung clinic for realistic multi-tenant data
UPDATE `users` SET `clinic_id` = 2 WHERE `id` IN (11, 21, 22, 30, 31, 32);
-- and a slice to the Surabaya practice
UPDATE `users` SET `clinic_id` = 3 WHERE `id` IN (12, 23, 33, 34);

-- ---------- assign children to the clinic of their doctor ----------
UPDATE `children` c
  JOIN `users` d ON d.id = c.doctor_id
  SET c.clinic_id = d.clinic_id
  WHERE c.clinic_id IS NULL AND d.clinic_id IS NOT NULL;

-- children whose doctor has no clinic fall back to clinic 1
UPDATE `children` SET `clinic_id` = 1 WHERE `clinic_id` IS NULL;

-- ---------- parents follow their child's clinic ----------
-- A parent whose children span several clinics keeps the lowest clinic id so the
-- parent scope stays deterministic and consistent with the child's clinic.
UPDATE `users` p
  JOIN (
    SELECT c.parent_id AS pid, MIN(c.clinic_id) AS clinic_id
    FROM `children` c
    WHERE c.parent_id IS NOT NULL AND c.clinic_id IS NOT NULL
    GROUP BY c.parent_id
  ) x ON x.pid = p.id
  SET p.clinic_id = x.clinic_id
  WHERE p.role = 'parent';

-- ---------- sample app versions ----------
INSERT INTO `app_versions`
  (`id`,`version`,`semver_major`,`semver_minor`,`semver_patch`,`release_notes`,`changelog`,`built_at`,`released_at`,`status`,`git_commit`,`git_branch`,`created_by`)
VALUES
(1,'1.0.0',1,0,0,
 'Rilis pertama TumbuhSehat: dashboard admin, penjadwalan sesi terapi, dan library video.',
 JSON_ARRAY(
   JSON_OBJECT('type','feature','text','Dashboard admin dengan KPI, matriks ketersediaan terapis, dan beban kerja'),
   JSON_OBJECT('type','feature','text','Penjadwalan sesi terapi otomatis dari program dokter'),
   JSON_OBJECT('type','feature','text','Laporan sesitherapy + parent report + WhatsApp share'),
   JSON_OBJECT('type','fix','text','Perbaikan perhitungan rata-rata fokus & kooperas')
 ),'2026-08-01 09:00:00','2026-08-05 10:00:00','deprecated','a1b2c3d4e5f60718293a4b5c6d7e8f9012345678','main',99),
(2,'1.1.0',1,1,0,
 'Penambahan modul progress therapy, riwayat milestone, dan permintaan reschedule dari orang tua.',
 JSON_ARRAY(
   JSON_OBJECT('type','feature','text','ProgressView dengan riwayat milestone per program'),
   JSON_OBJECT('type','feature','text','Permintaan reschedule dari orang tua & terapis'),
   JSON_OBJECT('type','fix','text','Slot otomatis tidak lagi menabrak sesi terjadwal')
 ),'2026-09-12 09:00:00','2026-09-18 10:00:00','deprecated','b2c3d4e5f60718293a4b5c6d7e8f901234567890','main',99),
(3,'2.0.0',2,0,0,
 'Rilis mayor: multi-entity (multi RS/Klinik), peran Admin Aplikasi (super_admin), dan sistem versi aplikasi.',
 JSON_ARRAY(
   JSON_OBJECT('type','feature','text','Multi-klinik: tabel clinics + clinic_id pada users & children'),
   JSON_OBJECT('type','feature','text','Peran baru super_admin (Admin Aplikasi) lintas seluruh klinik'),
   JSON_OBJECT('type','feature','text','Peran clinic_admin replaces admin, terlingkup satu klinik'),
   JSON_OBJECT('type','feature','text','Manajemen versi aplikasi: riwayat rilis, release notes, dan rollback'),
   JSON_OBJECT('type','breaking','text','Endpoint admin kini dibatasi per klinik; gunakan /api/clinics')
 ),'2026-11-01 09:00:00','2026-11-10 10:00:00','deprecated','c3d4e5f60718293a4b5c6d7e8f90123456789012','release/v3',99),
(4,'2.1.0',2,1,0,
 'Penyempurnaan dashboard admin aplikasi dan filter klinik pada seluruh data klinis.',
 JSON_ARRAY(
   JSON_OBJECT('type','feature','text','Dashboard lintas-klinik: total klinik, user, anak, sesi'),
   JSON_OBJECT('type','feature','text','Filter klinik pada modul users & children')
 ),'2026-11-11 09:00:00',NULL,'draft','d4e5f60718293a4b5c6d7e8f9012345678901234','develop',99)
ON DUPLICATE KEY UPDATE
  `release_notes` = VALUES(`release_notes`),
  `changelog` = VALUES(`changelog`),
  `status` = VALUES(`status`),
  `git_commit` = VALUES(`git_commit`),
  `git_branch` = VALUES(`git_branch`);

SET FOREIGN_KEY_CHECKS = 1;
