<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Teacher extends Model
{
    use HasFactory;

    protected $table = 'teachers';
    
    protected $fillable = [
        'teacher_id',
        'name_bn',
        'name_en',
        'father_name',
        'mother_name',
        'date_of_birth',
        'gender',
        'blood_group',
        'religion',
        'nationality',
        'nid_number',
        'birth_registration',
        'phone',
        'emergency_phone',
        'email',
        'present_address',
        'permanent_address',
        
        // Professional Info
        'qualification',
        'subject_specialization',
        'designation',
        'department',
        'joining_date',
        'employment_type', // permanent, probation, contract, part-time
        'job_status', // active, inactive, suspended, resigned
        'resign_date',
        
        // Salary Info
        'basic_salary',
        'house_rent',
        'medical_allowance',
        'transport_allowance',
        'other_allowance',
        'total_salary',
        'bank_name',
        'bank_account_no',
        'bkash_number',
        
        // Login Info
        'user_id',
        
        // Documents
        'photo',
        'cv',
        'certificates',
        'appointment_letter',
        
        // Others
        'training_history',
        'achievements',
        'remarks',
        'status'
    ];

    protected $casts = [
        'date_of_birth' => 'date',
        'joining_date' => 'date',
        'resign_date' => 'date',
        'basic_salary' => 'decimal:2',
        'house_rent' => 'decimal:2',
        'medical_allowance' => 'decimal:2',
        'transport_allowance' => 'decimal:2',
        'other_allowance' => 'decimal:2',
        'total_salary' => 'decimal:2',
        'status' => 'boolean'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function classes()
    {
        return $this->hasMany(Classes::class, 'teacher_id');
    }

    public function attendances()
    {
        return $this->hasMany(TeacherAttendance::class);
    }

    public function salaries()
    {
        return $this->hasMany(TeacherSalary::class);
    }

    public function leaves()
    {
        return $this->hasMany(TeacherLeave::class);
    }

    // Teacher ID Generate
    public static function generateTeacherId()
    {
        $year = date('Y');
        $lastTeacher = self::whereYear('created_at', $year)
                          ->orderBy('id', 'desc')
                          ->first();
        
        if ($lastTeacher) {
            $lastId = intval(substr($lastTeacher->teacher_id, -4));
            $newId = str_pad($lastId + 1, 4, '0', STR_PAD_LEFT);
        } else {
            $newId = '0001';
        }
        
        return 'TCH' . $year . $newId;
    }

    // Designations
    public static function getDesignations()
    {
        return [
            'headmaster' => 'প্রধান শিক্ষক',
            'assistant_headmaster' => 'সহকারী প্রধান শিক্ষক',
            'senior_teacher' => 'সিনিয়র শিক্ষক',
            'assistant_teacher' => 'সহকারী শিক্ষক',
            'demonstrator' => 'ডেমোনস্ট্রেটর',
            'lecturer' => 'লেকচারার',
            'instructor' => 'ইনস্ট্রাক্টর',
            'sports_teacher' => 'ক্রীড়া শিক্ষক',
            'music_teacher' => 'সংগীত শিক্ষক',
            'art_teacher' => 'চারুকলা শিক্ষক',
            'computer_teacher' => 'কম্পিউটার শিক্ষক',
            'lab_assistant' => 'ল্যাব সহকারী'
        ];
    }

    // Departments
    public static function getDepartments()
    {
        return [
            'bangla' => 'বাংলা',
            'english' => 'ইংরেজি',
            'math' => 'গণিত',
            'science' => 'বিজ্ঞান',
            'social_science' => 'সমাজ বিজ্ঞান',
            'religion' => 'ধর্ম শিক্ষা',
            'physical_education' => 'শারীরিক শিক্ষা',
            'arts' => 'চারুকলা',
            'music' => 'সংগীত',
            'computer' => 'কম্পিউটার'
        ];
    }

    // Employment Types
    public static function getEmploymentTypes()
    {
        return [
            'permanent' => 'স্থায়ী',
            'probation' => 'প্রোবেশন',
            'contract' => 'চুক্তিভিত্তিক',
            'part-time' => 'খণ্ডকালীন',
            'visiting' => 'ভিজিটিং'
        ];
    }

    // Job Status
    public static function getJobStatus()
    {
        return [
            'active' => 'সক্রিয়',
            'inactive' => 'নিষ্ক্রিয়',
            'suspended' => 'স্থগিত',
            'resigned' => 'ইস্তফা'
        ];
    }

    // Calculate total salary
    public function calculateTotalSalary()
    {
        return $this->basic_salary + 
               $this->house_rent + 
               $this->medical_allowance + 
               $this->transport_allowance + 
               $this->other_allowance;
    }

    // Age calculate
    public function getAgeAttribute()
    {
        if ($this->date_of_birth) {
            return $this->date_of_birth->age;
        }
        return null;
    }

    // Active teachers
    public function scopeActive($query)
    {
        return $query->where('job_status', 'active')
                     ->where('status', true);
    }
}