<?php
namespace App\Models\Qc;

use App\Models\BaseModel;

class ControlTestsModel extends BaseModel
{
    protected $table = 'control_tests';
    protected $primaryKey = 'control_test_id';
    protected $allowedFields = [
        'control_id',
        'test_id',
        'mean',
        'sd',
        'created_at',
        'updated_at',
        'deleted_at'
    ];
    protected $useTimestamps = true;
    protected $useSoftDeletes = true;

    public function search($keyword = null)
    {
        $builder = $this->db->table($this->table . ' ct');
        $builder->select('
            ct.control_test_id,
            ct.control_id,
            ct.test_id,
            ct.mean,
            ct.sd,
            c.control_name,
            c.lot,
            t.test_name,
            t.test_unit
        ');
        $builder->join('master_controls c', 'c.control_id = ct.control_id');
        $builder->join('master_tests t', 't.test_id = ct.test_id');
        $builder->where('ct.deleted_at', null);

        if ($keyword) {
            $builder->groupStart()
                ->like('c.control_name', $keyword)
                ->orLike('t.test_name', $keyword)
                ->orLike('c.lot', $keyword)
                ->groupEnd();
        }

        $builder->orderBy('c.control_name', 'ASC');
        $builder->orderBy('t.test_name', 'ASC');

        $rows = $builder->get()->getResultArray();
        return $this->snakeToCamelRecursive($rows);
    }

    /**
     * Get control-test with control and test details
     */
    public function getWithDetails(int $controlTestId): ?array
    {
        $builder = $this->db->table('control_tests ct');
        $builder->select('
            ct.control_test_id as id,
            ct.control_id as controlId,
            ct.test_id as testId,
            ct.mean,
            ct.sd,
            c.control_name as controlName,
            c.lot,
            t.test_name as testName,
            t.test_unit as testUnit
        ');
        $builder->join('master_controls c', 'c.control_id = ct.control_id');
        $builder->join('master_tests t', 't.test_id = ct.test_id');
        $builder->where('ct.control_test_id', $controlTestId);
        $builder->where('ct.deleted_at', null);

        return $builder->get()->getRowArray() ?: null;
    }

    /**
     * Get tests for a control with QC parameters
     */
    public function getByControl(int $controlId): array
    {
        $builder = $this->db->table('control_tests ct');
        $builder->select('
            ct.control_test_id as id,
            ct.control_id as controlId,
            ct.test_id as testId,
            ct.mean,
            ct.sd,
            t.test_name as testName,
            t.test_unit as testUnit
        ');
        $builder->join('master_tests t', 't.test_id = ct.test_id');
        $builder->where('ct.control_id', $controlId);
        $builder->where('ct.deleted_at', null);
        $builder->where('t.deleted_at', null);
        $builder->orderBy('t.test_name', 'ASC');

        return $builder->get()->getResultArray();
    }

    /**
     * Get controls for a test with QC parameters
     * Optionally filter by month to exclude expired controls
     */
    public function getByTest(int $testId, ?string $month = null): array
    {
        $builder = $this->db->table('control_tests ct');
        $builder->select('
            ct.control_test_id as id,
            ct.control_id as controlId,
            ct.test_id as testId,
            ct.mean,
            ct.sd,
            c.control_name as controlName,
            c.lot,
            c.producer,
            c.exp_date as expDate
        ');
        $builder->join('master_controls c', 'c.control_id = ct.control_id');
        $builder->where('ct.test_id', $testId);
        $builder->where('ct.deleted_at', null);
        $builder->where('c.deleted_at', null);

        // Filter out expired controls if month provided
        if ($month) {
            $monthEnd = $month . '-01';
            $builder->where('c.exp_date >=', $monthEnd);
        }

        $builder->orderBy('c.control_name', 'ASC');

        return $builder->get()->getResultArray();
    }

    /**
     * Get by control and test
     */
    public function getByControlAndTest(int $controlId, int $testId): ?array
    {
        $builder = $this->db->table('control_tests ct');
        $builder->select('
            ct.control_test_id as id,
            ct.control_id as controlId,
            ct.test_id as testId,
            ct.mean,
            ct.sd
        ');
        $builder->where('ct.control_id', $controlId);
        $builder->where('ct.test_id', $testId);
        $builder->where('ct.deleted_at', null);

        return $builder->get()->getRowArray() ?: null;
    }
}
