<?php

namespace App\Models;

use CodeIgniter\Model;

class TeacherModel extends Model
{
    protected $table            = 'teachers';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = true;
    protected $allowedFields    = [
        'user_id', 'nip', 'peg_id', 'golongan', 'jabatan', 'status_kepegawaian', 'is_sertifikasi', 'short_code', 'name', 'gender', 'phone', 'email', 'is_active', 'ai_provider', 'ai_api_key'
    ];

    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    public function getAllWithUser()
    {
        return $this->select('teachers.*, users.email as user_email, users.is_active as user_active, users.username as user_username')
                    ->join('users', 'users.id = teachers.user_id', 'left')
                    ->where('teachers.deleted_at', null)
                    ->orderBy("CASE WHEN teachers.short_code IS NULL THEN 1 WHEN teachers.short_code = '' THEN 1 ELSE 0 END ASC, CAST(teachers.short_code AS UNSIGNED) ASC, teachers.name ASC")
                    ->findAll();
    }

    public function getInactiveWithUser()
    {
        return $this->select('teachers.*, users.email as user_email, users.is_active as user_active, users.username as user_username')
                    ->join('users', 'users.id = teachers.user_id', 'left')
                    ->where('teachers.deleted_at IS NOT NULL')
                    ->withDeleted()
                    ->orderBy("CASE WHEN teachers.short_code IS NULL THEN 1 WHEN teachers.short_code = '' THEN 1 ELSE 0 END ASC, CAST(teachers.short_code AS UNSIGNED) ASC, teachers.name ASC")
                    ->findAll();
    }

    public function getWithUser($id)
    {
        return $this->select('teachers.*, users.email as login_email')
                    ->join('users', 'users.id = teachers.user_id', 'left')
                    ->where('teachers.id', $id)
                    ->where('teachers.deleted_at', null)
                    ->first();
    }

    // Cari guru berdasarkan short_code (untuk matching dengan aSc)
    public function findByShortCode($shortCode)
    {
        return $this->where('short_code', $shortCode)
                    ->where('deleted_at', null)
                    ->first();
    }

    // Ambil semua guru yang sudah punya short_code
    public function getWithShortCode()
    {
        return $this->where('short_code IS NOT NULL')
                    ->where('short_code !=', '')
                    ->where('deleted_at', null)
                    ->orderBy('short_code', 'ASC')
                    ->findAll();
    }

    // Ambil sebagai lookup: short_code => teacher data
    // Dipakai saat proses sync aSc → aplikasi
    public function getShortCodeLookup()
    {
        $teachers = $this->getWithShortCode();
        $lookup   = [];
        foreach ($teachers as $t) {
            $lookup[$t['short_code']] = $t;
        }
        return $lookup;
    }

    // Update short_code saja
    public function updateShortCode($id, $shortCode)
    {
        return $this->update($id, [
            'short_code' => $shortCode ?: null,
        ]);
    }
}