<?php

namespace App\Models;

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

class AdmissionApplication extends Model
{
    use HasFactory;

    protected $table = 'admission_applications';
    
    protected $fillable = [
        'application_no',
        'form_no',
        'academic_year',
        'class_id',
        'applicant_name_bn',
        'applicant_name_en',
        'date_of_birth',
        'birth_certificate_no',
        'gender',
        'blood_group',
        'religion',
        'nationality',
        'present_address',
        'permanent_address',
        'phone',
        'email',
        'father_name_bn',
        'father_name_en',
        'father_phone',
        'father_occupation',
        'father_education',
        'mother_name_bn',
        'mother_name_en',
        'mother_phone',
        'mother_occupation',
        'mother_education',
        'guardian_name',
        'guardian_relation',
        'guardian_phone',
        'previous_school',
        'previous_class',
        'previous_result',
        'transfer_certificate',
        'has_siblings',
        'sibling_details',
        'is_minority',
        'is_disabled',
        'disability_details',
        'documents',
        'photo',
        'signature',
        'birth_certificate',
        'status', // pending, shortlisted, selected, rejected, waiting
        'application_fee',
        'payment_status',
        'payment_id',
        'interview_date',
        'interview_time',
        'interview_result',
        'selected_rank',
        'waiting_number',
        'remarks',
        'submitted_by',
        'submitted_at'
    ];

    protected $casts = [
        'date_of_birth' => 'date',
        'documents' => 'array',
        'has_siblings' => 'boolean',
        'is_minority' => 'boolean',
        'is_disabled' => 'boolean',
        'application_fee' => 'decimal:2',
        'interview_date' => 'date',
        'interview_time' => 'datetime',
        'submitted_at' => 'datetime',
        'selected_rank' => 'integer',
        'waiting_number' => 'integer'
    ];

    public function class()
    {
        return $this->belongsTo(Classes::class, 'class_id');
    }

    public function payment()
    {
        return $this->belongsTo(Payment::class, 'payment_id');
    }

    // Application status
    public static function getStatusTypes()
    {
        return [
            'pending' => 'অপেক্ষমাণ',
            'shortlisted' => 'শর্টলিস্টেড',
            'selected' => 'নির্বাচিত',
            'rejected' => 'বাতিল',
            'waiting' => 'ওয়েটিং লিস্ট'
        ];
    }

    // Generate application number
    public static function generateApplicationNo()
    {
        $year = date('Y');
        $lastApp = self::whereYear('created_at', $year)
                      ->orderBy('id', 'desc')
                      ->first();
        
        if ($lastApp) {
            $lastNo = intval(substr($lastApp->application_no, -5));
            $newNo = str_pad($lastNo + 1, 5, '0', STR_PAD_LEFT);
        } else {
            $newNo = '00001';
        }
        
        return 'APP-' . $year . '-' . $newNo;
    }

    // Generate form number
    public static function generateFormNo()
    {
        return 'FORM-' . time() . rand(100, 999);
    }
}