<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class CmodQcSeeder extends Seeder
{
    public function run()
    {
        $this->seedDepts();
        $this->seedControls();
        $this->seedTests();
        $this->seedControlTests();
        $this->seedResults();
        $this->seedResultComments();
    }

    protected function seedDepts()
    {
        $depts = [
            ['dept_name' => 'Chemistry'],
            ['dept_name' => 'Hematology'],
            ['dept_name' => 'Immunology'],
            ['dept_name' => 'Urinalysis'],
        ];
        $this->db->table('master_depts')->insertBatch($depts);
    }

    protected function seedControls()
    {
        $controls = [
            ['dept_id' => 1, 'control_name' => 'QC Normal Chemistry', 'lot' => 'QC2024001', 'producer' => 'BioRad', 'exp_date' => '2025-12-31'],
            ['dept_id' => 1, 'control_name' => 'QC High Chemistry', 'lot' => 'QC2024002', 'producer' => 'BioRad', 'exp_date' => '2025-12-31'],
            ['dept_id' => 2, 'control_name' => 'QC Normal Hema', 'lot' => 'QC2024003', 'producer' => 'Streck', 'exp_date' => '2025-11-30'],
            ['dept_id' => 2, 'control_name' => 'QC Low Hema', 'lot' => 'QC2024004', 'producer' => 'Streck', 'exp_date' => '2025-11-30'],
            ['dept_id' => 3, 'control_name' => 'QC Normal Immuno', 'lot' => 'QC2024005', 'producer' => 'Roche', 'exp_date' => '2025-10-31'],
            ['dept_id' => 4, 'control_name' => 'QC Normal Urine', 'lot' => 'QC2024006', 'producer' => 'Siemens', 'exp_date' => '2025-09-30'],
            // New controls for January 2026
            ['dept_id' => 1, 'control_name' => 'Trulab N', 'lot' => 'TN2026001', 'producer' => 'Trinity', 'exp_date' => '2026-12-31'],
            ['dept_id' => 1, 'control_name' => 'Trulab P', 'lot' => 'TP2026001', 'producer' => 'Trinity', 'exp_date' => '2026-12-31'],
            ['dept_id' => 1, 'control_name' => 'Cholestest', 'lot' => 'CT2026001', 'producer' => 'Roche', 'exp_date' => '2026-12-31'],
        ];
        $this->db->table('master_controls')->insertBatch($controls);
    }

    protected function seedTests()
    {
        $tests = [
            ['dept_id' => 1, 'test_code' => 'GLU', 'test_name' => 'Glucose', 'test_unit' => 'mg/dL', 'test_method' => 'GOD-PAP', 'cva' => 5, 'ba' => 3, 'tea' => 10],
            ['dept_id' => 1, 'test_code' => 'CRE', 'test_name' => 'Creatinine', 'test_unit' => 'mg/dL', 'test_method' => 'Jaffe', 'cva' => 4, 'ba' => 2, 'tea' => 8],
            ['dept_id' => 1, 'test_code' => 'BUN', 'test_name' => 'Urea Nitrogen', 'test_unit' => 'mg/dL', 'test_method' => 'UREASE', 'cva' => 5, 'ba' => 3, 'tea' => 12],
            ['dept_id' => 1, 'test_code' => 'CHOL', 'test_name' => 'Cholesterol', 'test_unit' => 'mg/dL', 'test_method' => 'CHOD-PAP', 'cva' => 6, 'ba' => 4, 'tea' => 15],
            ['dept_id' => 2, 'test_code' => 'WBC', 'test_name' => 'WBC', 'test_unit' => 'x10^3/uL', 'test_method' => 'Impedance', 'cva' => 8, 'ba' => 5, 'tea' => 20],
            ['dept_id' => 2, 'test_code' => 'RBC', 'test_name' => 'RBC', 'test_unit' => 'x10^6/uL', 'test_method' => 'Impedance', 'cva' => 3, 'ba' => 2, 'tea' => 8],
            ['dept_id' => 2, 'test_code' => 'HGB', 'test_name' => 'Hemoglobin', 'test_unit' => 'g/dL', 'test_method' => 'Cyanmethemoglobin', 'cva' => 2, 'ba' => 1, 'tea' => 5],
            ['dept_id' => 3, 'test_code' => 'TSH', 'test_name' => 'TSH', 'test_unit' => 'mIU/L', 'test_method' => 'ECLIA', 'cva' => 10, 'ba' => 6, 'tea' => 25],
            ['dept_id' => 3, 'test_code' => 'FT4', 'test_name' => 'Free T4', 'test_unit' => 'ng/dL', 'test_method' => 'ECLIA', 'cva' => 8, 'ba' => 5, 'tea' => 20],
            ['dept_id' => 4, 'test_code' => 'UP', 'test_name' => 'Urine Protein', 'test_unit' => 'mg/dL', 'test_method' => 'Dipstick', 'cva' => 10, 'ba' => 8, 'tea' => 30],
        ];
        $this->db->table('master_tests')->insertBatch($tests);
    }

    protected function seedControlTests()
    {
        $controlTests = [
            ['control_id' => 1, 'test_id' => 1, 'mean' => 95, 'sd' => 5],
            ['control_id' => 1, 'test_id' => 2, 'mean' => 1.0, 'sd' => 0.05],
            ['control_id' => 1, 'test_id' => 3, 'mean' => 15, 'sd' => 1.2],
            ['control_id' => 2, 'test_id' => 1, 'mean' => 180, 'sd' => 12],
            ['control_id' => 2, 'test_id' => 2, 'mean' => 2.5, 'sd' => 0.15],
            ['control_id' => 2, 'test_id' => 4, 'mean' => 200, 'sd' => 15],
            ['control_id' => 3, 'test_id' => 5, 'mean' => 7.5, 'sd' => 0.6],
            ['control_id' => 3, 'test_id' => 6, 'mean' => 4.8, 'sd' => 0.2],
            ['control_id' => 3, 'test_id' => 7, 'mean' => 14.5, 'sd' => 0.5],
            ['control_id' => 4, 'test_id' => 5, 'mean' => 3.5, 'sd' => 0.3],
            ['control_id' => 4, 'test_id' => 6, 'mean' => 2.5, 'sd' => 0.15],
            ['control_id' => 5, 'test_id' => 8, 'mean' => 2.5, 'sd' => 0.3],
            ['control_id' => 5, 'test_id' => 9, 'mean' => 1.2, 'sd' => 0.1],
            ['control_id' => 6, 'test_id' => 10, 'mean' => 10, 'sd' => 1.5],
            ['control_id' => 1, 'test_id' => 4, 'mean' => 150, 'sd' => 10],
            // New control-tests for January 2026
            ['control_id' => 7, 'test_id' => 1, 'mean' => 90, 'sd' => 4],      // Trulab N - Glucose
            ['control_id' => 7, 'test_id' => 2, 'mean' => 0.9, 'sd' => 0.04],  // Trulab N - Creatinine
            ['control_id' => 7, 'test_id' => 4, 'mean' => 145, 'sd' => 8],     // Trulab N - Cholesterol
            ['control_id' => 8, 'test_id' => 1, 'mean' => 175, 'sd' => 10],    // Trulab P - Glucose
            ['control_id' => 8, 'test_id' => 2, 'mean' => 2.4, 'sd' => 0.12],  // Trulab P - Creatinine
            ['control_id' => 8, 'test_id' => 4, 'mean' => 195, 'sd' => 12],    // Trulab P - Cholesterol
            ['control_id' => 9, 'test_id' => 4, 'mean' => 180, 'sd' => 10],    // Cholestest - Cholesterol
        ];
        $this->db->table('control_tests')->insertBatch($controlTests);
    }

    protected function seedResults()
    {
        $faker = \Faker\Factory::create();
        $resultDate = '2026-01-01';
        $results = [];

        $controlTests = $this->db->table('control_tests')->get()->getResultArray();
        $resultCount = 0;

        foreach ($controlTests as $ct) {
            $numResults = $faker->numberBetween(3, 4);

            for ($i = 0; $i < $numResults && $resultCount < 50; $i++) {
                $resDate = date('Y-m-d', strtotime($resultDate . ' +' . $faker->numberBetween(0, 20) . ' days'));
                $value = $ct['mean'] + ($faker->randomFloat(2, -2.5, 2.5) * $ct['sd']);

                $results[] = [
                    'control_id' => $ct['control_id'],
                    'test_id' => $ct['test_id'],
                    'res_date' => $resDate,
                    'res_value' => round($value, 2),
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
                $resultCount++;
            }
        }
        $this->db->table('results')->insertBatch($results);
    }

    protected function seedResultComments()
    {
        // Get all results to associate comments with specific results
        $results = $this->db->table('results')->get()->getResultArray();

        if (empty($results)) {
            return;
        }

        // Map control_id + test_id to result_ids
        $resultMap = [];
        foreach ($results as $result) {
            $key = $result['control_id'] . '_' . $result['test_id'];
            $resultMap[$key][] = $result['result_id'];
        }

        // Comments data with control_id + test_id for mapping
        $commentsData = [
            ['control_id' => 1, 'test_id' => 1, 'comment_text' => 'Slight drift observed, instrument recalibrated on 01/15'],
            ['control_id' => 2, 'test_id' => 4, 'comment_text' => 'High cholesterol values noted, lot change recommended'],
            ['control_id' => 3, 'test_id' => 5, 'comment_text' => 'WBC controls stable throughout the month'],
            ['control_id' => 4, 'test_id' => 6, 'comment_text' => 'RBC QC intermittent shift, probe cleaned'],
            ['control_id' => 5, 'test_id' => 8, 'comment_text' => 'TSH assay maintenance performed on 01/10'],
            ['control_id' => 6, 'test_id' => 10, 'comment_text' => 'Urine protein controls within range'],
            ['control_id' => 1, 'test_id' => 2, 'comment_text' => 'Creatinine QC stable, no issues'],
            ['control_id' => 2, 'test_id' => 1, 'comment_text' => 'Glucose high QC showed consistent elevation, reagent lot changed'],
            ['control_id' => 3, 'test_id' => 7, 'comment_text' => 'Hemoglobin QC performance acceptable'],
            ['control_id' => 5, 'test_id' => 9, 'comment_text' => 'Free T4 calibration curve verified'],
            // New control comments for January 2026
            ['control_id' => 7, 'test_id' => 1, 'comment_text' => 'Trulab N Glucose stable throughout January'],
            ['control_id' => 7, 'test_id' => 2, 'comment_text' => 'Trulab N Creatinine within acceptable range'],
            ['control_id' => 7, 'test_id' => 4, 'comment_text' => 'Trulab N Cholesterol performance satisfactory'],
            ['control_id' => 8, 'test_id' => 1, 'comment_text' => 'Trulab P Glucose elevated, monitoring continued'],
            ['control_id' => 8, 'test_id' => 2, 'comment_text' => 'Trulab P Creatinine QC stable'],
            ['control_id' => 8, 'test_id' => 4, 'comment_text' => 'Trulab P Cholesterol consistent with expected values'],
            ['control_id' => 9, 'test_id' => 4, 'comment_text' => 'Cholestest performance verified, no issues'],
        ];

        $comments = [];
        foreach ($commentsData as $data) {
            $key = $data['control_id'] . '_' . $data['test_id'];
            if (isset($resultMap[$key]) && !empty($resultMap[$key])) {
                // Attach comment to the first matching result
                $comments[] = [
                    'result_id' => $resultMap[$key][0],
                    'comment_text' => $data['comment_text'],
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }
        }
        $this->db->table('result_comments')->insertBatch($comments);
    }
}
