<?php

namespace App\Models;

use App\Models\Concerns\BelongsToBranch;
use App\Models\Concerns\HasAuditColumns;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Inquiry extends Model
{
    use BelongsToBranch;
    use HasAuditColumns;
    use SoftDeletes;

    protected $table = 'inquiries';

    protected $fillable = [
        'branch_id', 'inquiry_number', 'student_name', 'parent_name', 'contact', 'email',
        'class_id', 'source', 'status', 'follow_up_date', 'notes',
    ];

    protected function casts(): array
    {
        return ['follow_up_date' => 'date'];
    }

    public const STATUSES = ['new', 'contacted', 'visited', 'converted', 'lost'];

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

    public function getStatusColorAttribute(): string
    {
        return [
            'new' => 'primary', 'contacted' => 'info', 'visited' => 'warning',
            'converted' => 'success', 'lost' => 'danger',
        ][$this->status] ?? 'secondary';
    }

    public static function nextNumber(int $branchId): string
    {
        $count = static::withoutGlobalScopes()->where('branch_id', $branchId)->withTrashed()->count();

        return 'INQ-'.date('Y').'-'.str_pad((string) ($count + 1), 4, '0', STR_PAD_LEFT);
    }
}
