<?php

namespace App\Models;

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

class AdmitCard extends Model
{
    use HasFactory;

    protected $table = 'admit_cards';
    
    protected $fillable = [
        'exam_id',
        'student_id',
        'admit_card_no',
        'issued_date',
        'is_printed',
        'printed_date',
        'verified_by',
        'status'
    ];

    protected $casts = [
        'issued_date' => 'date',
        'printed_date' => 'date',
        'is_printed' => 'boolean',
        'status' => 'boolean'
    ];

    public function exam()
    {
        return $this->belongsTo(Exam::class);
    }

    public function student()
    {
        return $this->belongsTo(Student::class);
    }

    public function verifier()
    {
        return $this->belongsTo(User::class, 'verified_by');
    }

    // Generate admit card number
    public static function generateAdmitCardNo($examId, $studentId)
    {
        $exam = Exam::find($examId);
        $student = Student::find($studentId);
        
        return 'AC-' . $exam->exam_year . '-' . 
               str_pad($examId, 3, '0', STR_PAD_LEFT) . '-' . 
               $student->student_id;
    }
}