<?php
declare(strict_types=1);

namespace App\Modules\Members\Models;

use App\Core\Model;
use App\Core\App;

class Member extends Model
{
    protected static string $table = 'members';
    protected static string $primaryKey = 'id';
    protected static bool $timestamps = true;
    protected static bool $softDelete = true;
    protected static bool $dispatchEvents = true;

    protected static array $fillable = [
        'membership_number', 'form_number', 'form_date', 'form_submission_id',
        'branch_id', 'membership_type', 'member_category', 'status',
        'full_name_ar', 'full_name_en', 'national_id', 'passport_number',
        'id_type', 'id_issue_date', 'id_expiry_date',
        'date_of_birth', 'age_years', 'age_months', 'gender',
        'place_of_birth', 'nationality', 'governorate_code',
        'religion', 'qualification_id', 'marital_status',
        'phone_home', 'phone_mobile', 'phone_international',
        'email', 'emergency_name', 'emergency_phone',
        'residence_type', 'residence_address', 'landmark', 'floor', 'apartment',
        'area', 'governorate', 'correspondence_address',
        'employment_type', 'occupation', 'job_title', 'employment_date',
        'business_address', 'office_phone', 'office_fax', 'business_activity',
        'membership_value', 'special_discount_id', 'special_discount_document', 'discount_amount',
        'payment_method', 'referral_source', 'photo_path',
        'workflow_instance_id',
    ];

    public function getBranchName(): string
    {
        if (!$this->branch_id) return '—';
        $db = App::getInstance()->db();
        $row = $db->selectOne("SELECT name_ar FROM branches WHERE id = ?", [$this->branch_id]);
        return $row['name_ar'] ?? '—';
    }

    public function getQualificationName(): string
    {
        if (!$this->qualification_id) return '—';
        $db = App::getInstance()->db();
        $row = $db->selectOne("SELECT name_ar FROM qualifications WHERE id = ?", [$this->qualification_id]);
        return $row['name_ar'] ?? '—';
    }

    public function getStatusLabel(): string
    {
        $labels = [
            'potential'          => 'عضوية محتملة',
            'under_review'       => 'تحت المراجعة',
            'interview_scheduled'=> 'في انتظار المقابلة',
            'accepted'           => 'مقبول',
            'rejected'           => 'مرفوض',
            'payment_pending'    => 'في انتظار السداد',
            'active'             => 'فعال',
            'frozen'             => 'مجمد',
            'suspended'          => 'موقوف',
            'dropped'            => 'مسقط',
            'expired'            => 'منتهي',
            'terminated'         => 'منتهي بقرار',
        ];
        return $labels[$this->status] ?? $this->status;
    }

    public function getStatusColor(): string
    {
        $colors = [
            'potential'           => '#6B7280',
            'under_review'        => '#D97706',
            'interview_scheduled' => '#0284C7',
            'accepted'            => '#059669',
            'rejected'            => '#DC2626',
            'payment_pending'     => '#D97706',
            'active'              => '#059669',
            'frozen'              => '#6B7280',
            'suspended'           => '#DC2626',
            'dropped'             => '#DC2626',
            'expired'             => '#9CA3AF',
            'terminated'          => '#DC2626',
        ];
        return $colors[$this->status] ?? '#6B7280';
    }

    public static function getStatusOptions(): array
    {
        return [
            'potential'           => 'عضوية محتملة',
            'under_review'        => 'تحت المراجعة',
            'interview_scheduled' => 'في انتظار المقابلة',
            'accepted'            => 'مقبول',
            'rejected'            => 'مرفوض',
            'payment_pending'     => 'في انتظار السداد',
            'active'              => 'فعال',
            'frozen'              => 'مجمد',
            'suspended'           => 'موقوف',
            'dropped'             => 'مسقط',
            'expired'             => 'منتهي',
        ];
    }

    public static function getMembershipTypes(): array
    {
        return [
            'working'   => 'عامل',
            'seasonal'  => 'موسمي',
            'sports'    => 'رياضي',
            'honorary'  => 'شرفي',
            'foreign'   => 'أجنبي',
        ];
    }

    public function getNotes(): array
    {
        $db = App::getInstance()->db();
        return $db->select(
            "SELECT mn.*, e.full_name_ar as employee_name FROM member_notes mn LEFT JOIN employees e ON e.id = mn.created_by WHERE mn.member_id = ? ORDER BY mn.created_at DESC",
            [$this->id]
        );
    }

    public function getGenderLabel(): string
    {
        return $this->gender === 'male' ? 'ذكر' : ($this->gender === 'female' ? 'أنثى' : $this->gender);
    }
}