<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class DummySeeder extends Seeder
{
  public function run()
  {
    $now = date('Y-m-d H:i:s');
    $yesterday = date('Y-m-d H:i:s', strtotime('-1 day'));
    $twoDaysAgo = date('Y-m-d H:i:s', strtotime('-2 days'));
    $threeDaysAgo = date('Y-m-d H:i:s', strtotime('-3 days'));

    // users
    // Password: 'password' for all users (bcrypt hash)
    $passwordHash = password_hash('123', PASSWORD_BCRYPT);
    $data = [
      ['id' => 1, 'role_id' => 1, 'username' => 'lisfse', 'password' => $passwordHash],
      ['id' => 2, 'role_id' => 1, 'username' => 'tes', 'password' => $passwordHash]
    ];
    $this->db->table('users')->insertBatch($data);

    // patvisit - 4 visits for 3 patients
    // Visit 1: Patient 1 - Discharged (complete history)
    // Visit 2: Patient 1 - Discharged (complete history)  
    // Visit 3: Patient 2 - Active (no discharge yet)
    // Visit 4: Patient 3 - Discharged (complete history)
    $data = [
      ['InternalPVID' => 1, "PVID" => "DV00001", "InternalPID" => 1, "EpisodeID" => 1, "CreateDate" => "$threeDaysAgo"],
      ['InternalPVID' => 2, "PVID" => "DV00002", "InternalPID" => 1, "EpisodeID" => 1, "CreateDate" => "$twoDaysAgo"],
      ['InternalPVID' => 3, "PVID" => "DV00003", "InternalPID" => 2, "EpisodeID" => 2, "CreateDate" => "$yesterday"],
      ['InternalPVID' => 4, "PVID" => "DV00004", "InternalPID" => 3, "EpisodeID" => 3, "CreateDate" => "$threeDaysAgo"],
    ];
    $this->db->table('patvisit')->insertBatch($data);

    // patvisitadt - ADT history for each visit
    // A04: Register, A01: Admit, A02: Transfer, A03: Discharge
    $data = [
      // Visit 1 (InternalPVID=1) - Discharged: ER -> Ward A -> Ward B -> Discharge
      ['InternalPVID' => 1, "ADTCode" => "A04", "LocationID" => 1, "AttDoc" => 1, "RefDoc" => null, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => "$threeDaysAgo", "EndDate" => null],
      ['InternalPVID' => 1, "ADTCode" => "A01", "LocationID" => 1, "AttDoc" => 1, "RefDoc" => null, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => date('Y-m-d H:i:s', strtotime('-2 days +2 hours')), "EndDate" => null],
      ['InternalPVID' => 1, "ADTCode" => "A02", "LocationID" => 2, "AttDoc" => 1, "RefDoc" => 2, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => date('Y-m-d H:i:s', strtotime('-2 days +6 hours')), "EndDate" => null],
      ['InternalPVID' => 1, "ADTCode" => "A02", "LocationID" => 2, "AttDoc" => 2, "RefDoc" => 2, "AdmDoc" => 1, "CnsDoc" => 2, "CreateDate" => date('Y-m-d H:i:s', strtotime('-1 day +8 hours')), "EndDate" => null],
      ['InternalPVID' => 1, "ADTCode" => "A03", "LocationID" => null, "AttDoc" => 2, "RefDoc" => 2, "AdmDoc" => 1, "CnsDoc" => 2, "CreateDate" => date('Y-m-d H:i:s', strtotime('-1 day +12 hours')), "EndDate" => null],

      // Visit 2 (InternalPVID=2) - Discharged: ER -> ICU -> Discharge
      ['InternalPVID' => 2, "ADTCode" => "A04", "LocationID" => 1, "AttDoc" => 1, "RefDoc" => null, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => "$twoDaysAgo", "EndDate" => null],
      ['InternalPVID' => 2, "ADTCode" => "A01", "LocationID" => 1, "AttDoc" => 1, "RefDoc" => null, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => date('Y-m-d H:i:s', strtotime('-1 day +1 hour')), "EndDate" => null],
      ['InternalPVID' => 2, "ADTCode" => "A02", "LocationID" => 2, "AttDoc" => 2, "RefDoc" => 1, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => date('Y-m-d H:i:s', strtotime('-1 day +4 hours')), "EndDate" => null],
      ['InternalPVID' => 2, "ADTCode" => "A03", "LocationID" => null, "AttDoc" => 2, "RefDoc" => 1, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => "$yesterday", "EndDate" => null],

      // Visit 3 (InternalPVID=3) - ACTIVE: ER -> Ward A (no discharge - still admitted)
      ['InternalPVID' => 3, "ADTCode" => "A04", "LocationID" => 1, "AttDoc" => 1, "RefDoc" => null, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => "$yesterday", "EndDate" => null],
      ['InternalPVID' => 3, "ADTCode" => "A01", "LocationID" => 1, "AttDoc" => 1, "RefDoc" => null, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => date('Y-m-d H:i:s', strtotime('-1 day +2 hours')), "EndDate" => null],
      ['InternalPVID' => 3, "ADTCode" => "A02", "LocationID" => 2, "AttDoc" => 2, "RefDoc" => 2, "AdmDoc" => 1, "CnsDoc" => null, "CreateDate" => "$now", "EndDate" => null],
      // Note: No A03 (discharge) - this visit is still active

      // Visit 4 (InternalPVID=4) - Discharged: Direct admit -> Ward B -> Discharge
      ['InternalPVID' => 4, "ADTCode" => "A04", "LocationID" => 2, "AttDoc" => 2, "RefDoc" => null, "AdmDoc" => 2, "CnsDoc" => null, "CreateDate" => "$threeDaysAgo", "EndDate" => null],
      ['InternalPVID' => 4, "ADTCode" => "A01", "LocationID" => 2, "AttDoc" => 2, "RefDoc" => null, "AdmDoc" => 2, "CnsDoc" => null, "CreateDate" => date('Y-m-d H:i:s', strtotime('-2 days +3 hours')), "EndDate" => null],
      ['InternalPVID' => 4, "ADTCode" => "A02", "LocationID" => 1, "AttDoc" => 1, "RefDoc" => 2, "AdmDoc" => 2, "CnsDoc" => 1, "CreateDate" => date('Y-m-d H:i:s', strtotime('-2 days +12 hours')), "EndDate" => null],
      ['InternalPVID' => 4, "ADTCode" => "A03", "LocationID" => null, "AttDoc" => 1, "RefDoc" => 2, "AdmDoc" => 2, "CnsDoc" => 1, "CreateDate" => date('Y-m-d H:i:s', strtotime('-1 day +6 hours')), "EndDate" => null],
    ];
    $this->db->table('patvisitadt')->insertBatch($data);

    echo "✅ Dummy ADT data seeded successfully!\n";
    echo "-------------------------------------------\n";
    echo "Visits created: 4\n";
    echo "  - Visit 1 (DV00001): Complete ADT history with discharge\n";
    echo "  - Visit 2 (DV00002): Complete ADT history with discharge\n";
    echo "  - Visit 3 (DV00003): ACTIVE (no discharge yet)\n";
    echo "  - Visit 4 (DV00004): Complete ADT history with discharge\n";
    echo "-------------------------------------------\n";
    echo "ADT Records: 18 total\n";
    echo "  - A04 (Register): 4 records\n";
    echo "  - A01 (Admit): 4 records\n";
    echo "  - A02 (Transfer): 6 records\n";
    echo "  - A03 (Discharge): 3 records\n";
    echo "-------------------------------------------\n";
  }
}