<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

/**
 * PatientTestSeeder
 * 
 * Seeds test data for Patient-related tables for PHPUnit testing.
 * This includes: patient, patatt, patcom, patidt, valueset, areageo
 * 
 * Run with: php spark db:seed PatientTestSeeder
 * Or for test DB: php spark db:seed PatientTestSeeder --all --n tests
 */
class PatientSeeder extends Seeder
{
  public function run()
  {
    $now = date('Y-m-d H:i:s');

    // ========================================
    // 1. VALUESET - Required for joins
    // ========================================
    $valuesets = [
      // Gender (VSetID = 1)
      ['VID' => 5, 'SiteID' => 1, 'VSetID' => 1, 'VOrder' => 1, 'VValue' => 'M', 'VDesc' => 'Male', 'CreateDate' => $now],
      ['VID' => 6, 'SiteID' => 1, 'VSetID' => 1, 'VOrder' => 2, 'VValue' => 'F', 'VDesc' => 'Female', 'CreateDate' => $now],
      ['VID' => 7, 'SiteID' => 1, 'VSetID' => 1, 'VOrder' => 3, 'VValue' => 'O', 'VDesc' => 'Other', 'CreateDate' => $now],

      // Marital Status (VSetID = 3)
      ['VID' => 8, 'SiteID' => 1, 'VSetID' => 3, 'VOrder' => 1, 'VValue' => 'S', 'VDesc' => 'Single', 'CreateDate' => $now],
      ['VID' => 9, 'SiteID' => 1, 'VSetID' => 3, 'VOrder' => 2, 'VValue' => 'M', 'VDesc' => 'Married', 'CreateDate' => $now],
      ['VID' => 10, 'SiteID' => 1, 'VSetID' => 3, 'VOrder' => 3, 'VValue' => 'D', 'VDesc' => 'Divorced', 'CreateDate' => $now],
      ['VID' => 11, 'SiteID' => 1, 'VSetID' => 3, 'VOrder' => 4, 'VValue' => 'W', 'VDesc' => 'Widowed', 'CreateDate' => $now],

      // Death Indicator (VSetID = 4)
      ['VID' => 16, 'SiteID' => 1, 'VSetID' => 4, 'VOrder' => 1, 'VValue' => 'Y', 'VDesc' => 'Deceased', 'CreateDate' => $now],
      ['VID' => 17, 'SiteID' => 1, 'VSetID' => 4, 'VOrder' => 2, 'VValue' => 'N', 'VDesc' => 'Alive', 'CreateDate' => $now],

      // Race (VSetID = 5)
      ['VID' => 175, 'SiteID' => 1, 'VSetID' => 5, 'VOrder' => 1, 'VValue' => 'AS', 'VDesc' => 'Asian', 'CreateDate' => $now],
      ['VID' => 176, 'SiteID' => 1, 'VSetID' => 5, 'VOrder' => 2, 'VValue' => 'WH', 'VDesc' => 'White', 'CreateDate' => $now],

      // Religion (VSetID = 6)
      ['VID' => 206, 'SiteID' => 1, 'VSetID' => 6, 'VOrder' => 1, 'VValue' => 'IS', 'VDesc' => 'Islam', 'CreateDate' => $now],
      ['VID' => 207, 'SiteID' => 1, 'VSetID' => 6, 'VOrder' => 2, 'VValue' => 'CH', 'VDesc' => 'Christian', 'CreateDate' => $now],
      ['VID' => 208, 'SiteID' => 1, 'VSetID' => 6, 'VOrder' => 3, 'VValue' => 'CA', 'VDesc' => 'Catholic', 'CreateDate' => $now],
      ['VID' => 209, 'SiteID' => 1, 'VSetID' => 6, 'VOrder' => 4, 'VValue' => 'HI', 'VDesc' => 'Hindu', 'CreateDate' => $now],
      ['VID' => 210, 'SiteID' => 1, 'VSetID' => 6, 'VOrder' => 5, 'VValue' => 'BU', 'VDesc' => 'Buddha', 'CreateDate' => $now],

      // Ethnic (VSetID = 7)
      ['VID' => 213, 'SiteID' => 1, 'VSetID' => 7, 'VOrder' => 1, 'VValue' => 'JV', 'VDesc' => 'Javanese', 'CreateDate' => $now],
      ['VID' => 214, 'SiteID' => 1, 'VSetID' => 7, 'VOrder' => 2, 'VValue' => 'SD', 'VDesc' => 'Sundanese', 'CreateDate' => $now],
      ['VID' => 215, 'SiteID' => 1, 'VSetID' => 7, 'VOrder' => 3, 'VValue' => 'BT', 'VDesc' => 'Batak', 'CreateDate' => $now],

      // Country (VSetID = 8)
      ['VID' => 221, 'SiteID' => 1, 'VSetID' => 8, 'VOrder' => 1, 'VValue' => 'ID', 'VDesc' => 'Indonesia', 'CreateDate' => $now],
      ['VID' => 222, 'SiteID' => 1, 'VSetID' => 8, 'VOrder' => 2, 'VValue' => 'MY', 'VDesc' => 'Malaysia', 'CreateDate' => $now],
      ['VID' => 223, 'SiteID' => 1, 'VSetID' => 8, 'VOrder' => 3, 'VValue' => 'SG', 'VDesc' => 'Singapore', 'CreateDate' => $now],

      // Link Type (VSetID = 9)
      ['VID' => 2, 'SiteID' => 1, 'VSetID' => 9, 'VOrder' => 1, 'VValue' => 'F', 'VDesc' => 'Family', 'CreateDate' => $now],
      ['VID' => 3, 'SiteID' => 1, 'VSetID' => 9, 'VOrder' => 2, 'VValue' => 'S', 'VDesc' => 'Spouse', 'CreateDate' => $now],
    ];

    // Insert valuesets (ignore duplicates)
    foreach ($valuesets as $vs) {
      $exists = $this->db->table('valueset')->where('VID', $vs['VID'])->get()->getRow();
      if (!$exists) {
        $this->db->table('valueset')->insert($vs);
      }
    }
    echo "Valueset data seeded.\n";

    // ========================================
    // 2. PATIENT - Main patient data
    // ========================================
    $patients = [
      [
        'InternalPID' => 1,
        'PatientID' => 'SMAJ1',
        'AlternatePID' => 'ALT001',
        'Prefix' => 'Mr.',
        'NameFirst' => 'Dummy',
        'NameMiddle' => 'Test',
        'NameMaiden' => null,
        'NameLast' => 'Patient',
        'Suffix' => 'S.Kom',
        'NameAlias' => 'DummyTest',
        'Gender' => 5,
        'PlaceOfBirth' => 'Jakarta',
        'Birthdate' => '1990-05-15',
        'Street_1' => 'Jl. Sudirman No. 123',
        'Street_2' => 'RT 01 RW 02',
        'Street_3' => 'Kelurahan Menteng',
        'City' => '2',
        'Province' => '1',
        'ZIP' => '10110',
        'EmailAddress1' => 'dummy1@test.com',
        'EmailAddress2' => 'dummy1alt@test.com',
        'Phone' => '02112345678',
        'MobilePhone' => '081234567890',
        'Custodian' => null,
        'AccountNumber' => null,
        'Country' => 221,
        'Race' => 175,
        'MaritalStatus' => 9,
        'Religion' => 206,
        'Ethnic' => 213,
        'Citizenship' => 'WNI',
        'DeathIndicator' => 17,
        'TimeOfDeath' => null,
        'LinkTo' => null,
        'CreateDate' => $now,
        'DelDate' => null
      ],
      [
        'InternalPID' => 2,
        'PatientID' => 'PAT002',
        'AlternatePID' => 'ALT002',
        'Prefix' => 'Mrs.',
        'NameFirst' => 'Jane',
        'NameMiddle' => null,
        'NameMaiden' => 'Smith',
        'NameLast' => 'Doe',
        'Suffix' => null,
        'NameAlias' => 'JaneDoe',
        'Gender' => 6, // Female
        'PlaceOfBirth' => 'Bandung',
        'Birthdate' => '1985-10-20',
        'Street_1' => 'Jl. Asia Afrika No. 456',
        'Street_2' => 'RT 03 RW 04',
        'Street_3' => null,
        'City' => '8', // Bandung
        'Province' => '7', // Jawa Barat
        'ZIP' => '40112',
        'EmailAddress1' => 'jane.doe@test.com',
        'EmailAddress2' => null,
        'Phone' => '02298765432',
        'MobilePhone' => '089876543210',
        'Custodian' => null,
        'AccountNumber' => null,
        'Country' => 221,
        'Race' => 175,
        'MaritalStatus' => 8, // Single
        'Religion' => 207, // Christian
        'Ethnic' => 214, // Sundanese
        'Citizenship' => 'WNI',
        'DeathIndicator' => 17,
        'TimeOfDeath' => null,
        'LinkTo' => null,
        'CreateDate' => $now,
        'DelDate' => null
      ],
      [
        'InternalPID' => 3,
        'PatientID' => 'PAT003',
        'AlternatePID' => 'ALT003',
        'Prefix' => 'Mr.',
        'NameFirst' => 'Budi',
        'NameMiddle' => 'Santoso',
        'NameMaiden' => null,
        'NameLast' => 'Wijaya',
        'Suffix' => null,
        'NameAlias' => 'BudiW',
        'Gender' => 5,
        'PlaceOfBirth' => 'Surabaya',
        'Birthdate' => '2000-01-01',
        'Street_1' => 'Jl. Pahlawan No. 789',
        'Street_2' => 'RT 05 RW 06',
        'Street_3' => null,
        'City' => '2',
        'Province' => '1',
        'ZIP' => '10220',
        'EmailAddress1' => 'budi.wijaya@test.com',
        'EmailAddress2' => null,
        'Phone' => null,
        'MobilePhone' => '081111222333',
        'Custodian' => 1,
        'AccountNumber' => null,
        'Country' => 221,
        'Race' => 175,
        'MaritalStatus' => 8,
        'Religion' => 206,
        'Ethnic' => 213,
        'Citizenship' => 'WNI',
        'DeathIndicator' => 17,
        'TimeOfDeath' => null,
        'LinkTo' => '1,2',
        'CreateDate' => $now,
        'DelDate' => null
      ],
    ];

    foreach ($patients as $patient) {
      $exists = $this->db->table('patient')->where('InternalPID', $patient['InternalPID'])->get()->getRow();
      if (!$exists) {
        $this->db->table('patient')->insert($patient);
      }
    }
    echo "Patient data seeded (3 patients).\n";

    // ========================================
    // 4. PATIDT - Patient Identifiers (KTP, etc.)
    // ========================================
    $patidts = [
      [
        'PatIdtID' => 1,
        'InternalPID' => 1,
        'IdentifierType' => 'KTP',
        'Identifier' => '3171234567890001',
        'EffectiveDate' => '2015-01-01',
        'ExpirationDate' => '2025-01-01',
        'CreateDate' => $now,
        'DelDate' => null
      ],
      [
        'PatIdtID' => 2,
        'InternalPID' => 3,
        'IdentifierType' => 'SIM',
        'Identifier' => '1234567890123456789',
        'EffectiveDate' => '2020-06-15',
        'ExpirationDate' => '2025-06-15',
        'CreateDate' => $now,
        'DelDate' => null
      ],
    ];

    foreach ($patidts as $patidt) {
      $exists = $this->db->table('patidt')->where('PatIdtID', $patidt['PatIdtID'])->get()->getRow();
      if (!$exists) {
        $this->db->table('patidt')->insert($patidt);
      }
    }
    echo "PatIdt data seeded (2 identifiers).\n";

    // ========================================
    // 5. PATATT - Patient Attachments
    // ========================================
    $patatts = [
      [
        'PatAttID' => 1,
        'InternalPID' => 1,
        'Address' => '/api/upload/patient1_photo1.jpg',
        'UserID' => 1,
        'CreateDate' => $now,
        'DelDate' => null
      ],
      [
        'PatAttID' => 2,
        'InternalPID' => 1,
        'Address' => '/api/upload/patient1_ktp.jpg',
        'UserID' => 1,
        'CreateDate' => $now,
        'DelDate' => null
      ],
      [
        'PatAttID' => 3,
        'InternalPID' => 3,
        'Address' => '/api/upload/patient3_photo.jpg',
        'UserID' => 1,
        'CreateDate' => $now,
        'DelDate' => null
      ],
    ];

    foreach ($patatts as $patatt) {
      $exists = $this->db->table('patatt')->where('PatAttID', $patatt['PatAttID'])->get()->getRow();
      if (!$exists) {
        $this->db->table('patatt')->insert($patatt);
      }
    }
    echo "PatAtt data seeded (3 attachments).\n";

    // ========================================
    // 6. PATCOM - Patient Comments
    // ========================================
    $patcoms = [
      [
        'PatComID' => 1,
        'InternalPID' => 1,
        'Comment' => 'Test patient for unit testing. Has complete data including ID and attachments.',
        'CreateDate' => $now,
        'EndDate' => null
      ],
      [
        'PatComID' => 2,
        'InternalPID' => 3,
        'Comment' => 'Patient with custodian relationship to patient 1.',
        'CreateDate' => $now,
        'EndDate' => null
      ],
    ];

    foreach ($patcoms as $patcom) {
      $exists = $this->db->table('patcom')->where('PatComID', $patcom['PatComID'])->get()->getRow();
      if (!$exists) {
        $this->db->table('patcom')->insert($patcom);
      }
    }
    echo "PatCom data seeded (2 comments).\n";

    echo "\n✅ All Patient test data seeded successfully!\n";
    echo "-------------------------------------------\n";
    echo "Patients: 3 (IDs: 1, 2, 3)\n";
    echo "  - Patient 1: Full data with PatIdt, PatAtt (2), PatCom\n";
    echo "  - Patient 2: No PatIdt, PatAtt, PatCom (for null testing)\n";
    echo "  - Patient 3: Has PatIdt, PatAtt, PatCom, and Custodian link\n";
    echo "-------------------------------------------\n";
  }
}
