<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class PatientSeeder extends Seeder
{
  public function run()
  {
    $now = date('Y-m-d H:i:s');

    // ========================================
    // 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',
        'Sex' => '1',
        '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' => 'IDN',
        'Race' => 'JAWA',
        'MaritalStatus' => 'M',
        'Religion' => 'ISLAM',
        'Ethnic' => 'TNGHA',
        'Citizenship' => 'WNI',
        'isDead' => '0',
        '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',
        'Sex' => '2',
        'PlaceOfBirth' => 'Bandung',
        'Birthdate' => '1985-10-20',
        'Street_1' => 'Jl. Asia Afrika No. 456',
        'Street_2' => 'RT 03 RW 04',
        'Street_3' => null,
        'City' => '8',
        'Province' => '7',
        'ZIP' => '40112',
        'EmailAddress1' => 'jane.doe@test.com',
        'EmailAddress2' => null,
        'Phone' => '02298765432',
        'MobilePhone' => '089876543210',
        'Custodian' => null,
        'AccountNumber' => null,
        'Country' => 'IDN',
        'Race' => 'JAWA',
        'MaritalStatus' => 'S',
        'Religion' => 'KRSTN',
        'Ethnic' => 'TNGHA',
        'Citizenship' => 'WNI',
        'isDead' => '0',
        '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',
        'Sex' => '1',
        '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' => 'IDN',
        'Race' => 'JAWA',
        'MaritalStatus' => 'S',
        'Religion' => 'ISLAM',
        'Ethnic' => 'TNGHA',
        'Citizenship' => 'WNI',
        'isDead' => '0',
        '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";

    // ========================================
    // 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";

    // ========================================
    // 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";

    // ========================================
    // 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";
  }
}
