<?php

namespace App\Models;

use CodeIgniter\Model;

class MemberModel extends Model
{
    protected $table = 'member';
    protected $primaryKey = 'id';
    protected $useAutoIncrement = true;
    protected $returnType = 'array';
    protected $useSoftDeletes = false;
    protected $allowedFields = [
        'user_id', 'full_name', 'roll_no', 'chapter', 'email', 'phone',
        'gender', 'date_of_birth', 'date_of_death', 'admission_date', 'status', 'category',
        'fees_year', 'prev_fees_year', 'picture', 'medical', 'insurance_cover',
        'insurance_expiry', 'spouse', 'passport', 'national_id',
        'marital_status', 'specialization', 'position', 'complied',
        'certissue', 'certexpiry', 'idstatus', 'idexpiry', 'firm_name',
        'firm_location', 'firm_tin', 'firm_address', 'firm_category', 'cv',
        'statusMod'
    ];
    protected $useTimestamps = false;

    protected $validationRules = [
        'roll_no' => 'required|is_unique[member.roll_no,id,{id}]',
        'full_name' => 'required',
    ];

    public function getMemberWithDetails($memberId)
    {
        return $this->find($memberId);
    }

    public function getMemberByRollNo($rollNo)
    {
        return $this->where('roll_no', $rollNo)->first();
    }

    public function getMemberByUserId($userId)
    {
        return $this->where('user_id', $userId)->first();
    }

    public function getActiveMembersCount()
    {
        return $this->where('status', 'active')->countAllResults();
    }

    public function getDeceasedMembersCount()
    {
        return $this->where('status', 'deceased')->countAllResults();
    }

    public function searchMembers($search)
    {
        return $this->like('full_name', $search)
            ->orLike('roll_no', $search)
            ->orLike('email', $search)
            ->findAll();
    }
}
