<?php

namespace App\Models;

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

class Committee extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'committee_id',
        'committee_name',
        'start_date',
        'end_date',
        'status',
        'created_by',
        'remarks'
    ];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
    ];

    protected $dates = ['deleted_at'];

    // স্বয়ংক্রিয় কমিটি আইডি জেনারেটর
    public static function generateCommitteeId()
    {
        $year = date('Y');
        $nextYear = date('Y') + 4;
        $prefix = 'COM';
        
        $lastCommittee = self::where('committee_id', 'like', $prefix . '-' . $year . '-%')
            ->orderBy('committee_id', 'desc')
            ->first();
        
        if ($lastCommittee) {
            $lastNumber = intval(substr($lastCommittee->committee_id, -2));
            $nextNumber = str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT);
        } else {
            $nextNumber = '01';
        }
        
        return $prefix . '-' . $year . '-' . $nextYear . '-' . $nextNumber;
    }

    // বুট মেথড
    protected static function boot()
    {
        parent::boot();
        
        static::creating(function ($committee) {
            if (empty($committee->committee_id)) {
                $committee->committee_id = self::generateCommitteeId();
            }
            
            if (empty($committee->committee_name)) {
                $committee->committee_name = 'কমিটি ' . date('Y') . '-' . (date('Y') + 4);
            }
            
            if (empty($committee->end_date)) {
                $committee->end_date = now()->addYears(4);
            }
        });
    }

    // রিলেশনশিপ: কমিটি সদস্য
    public function members()
    {
        return $this->hasMany(CommitteeMember::class, 'committee_id', 'committee_id');
    }

    // রিলেশনশিপ: সক্রিয় সদস্য
    public function activeMembers()
    {
        return $this->hasMany(CommitteeMember::class, 'committee_id', 'committee_id')
            ->where('status', 'active');
    }

    // রিলেশনশিপ: সভাপতি
    public function president()
    {
        return $this->hasOne(CommitteeMember::class, 'committee_id', 'committee_id')
            ->where('designation', 'সভাপতি')
            ->where('status', 'active');
    }

    // রিলেশনশিপ: সম্পাদক
    public function secretary()
    {
        return $this->hasOne(CommitteeMember::class, 'committee_id', 'committee_id')
            ->where('designation', 'সম্পাদক')
            ->where('status', 'active');
    }

    // রিলেশনশিপ: কোষাধ্যক্ষ
    public function treasurer()
    {
        return $this->hasOne(CommitteeMember::class, 'committee_id', 'committee_id')
            ->where('designation', 'কোষাধ্যক্ষ')
            ->where('status', 'active');
    }

    // কমিটি স্ট্যাটাস চেক
    public function getIsActiveAttribute()
    {
        return $this->status == 'active' && $this->end_date >= now();
    }

    // কমিটি স্ট্যাটাস বাংলায়
    public function getStatusInBanglaAttribute()
    {
        $statuses = [
            'active' => 'সক্রিয়',
            'expired' => 'মেয়াদোত্তীর্ণ'
        ];
        
        return $statuses[$this->status] ?? $this->status;
    }

    // বাকি দিন
    public function getRemainingDaysAttribute()
    {
        if ($this->end_date < now()) {
            return 0;
        }
        
        return now()->diffInDays($this->end_date);
    }

    // মেয়াদকাল
    public function getTenureAttribute()
    {
        $start = $this->start_date->format('d/m/Y');
        $end = $this->end_date->format('d/m/Y');
        
        return $start . ' - ' . $end;
    }
}