<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

class AgencyManagementReport extends Model
{
    use HasFactory;

    protected $fillable = [
        'period_type',
        'date_from',
        'date_to',
        'status',
        'overall_score',
        'summary',
        'kpis',
        'risk_clients',
        'top_clients',
        'team_workload',
        'channel_health',
        'recommendations',
        'html_path',
        'csv_path',
        'generated_at',
        'generated_by',
        'error_message',
    ];

    protected $casts = [
        'date_from' => 'date',
        'date_to' => 'date',
        'summary' => 'array',
        'kpis' => 'array',
        'risk_clients' => 'array',
        'top_clients' => 'array',
        'team_workload' => 'array',
        'channel_health' => 'array',
        'recommendations' => 'array',
        'generated_at' => 'datetime',
    ];

    public function htmlExists(): bool
    {
        return $this->html_path !== null && Storage::disk('public')->exists($this->html_path);
    }

    public function csvExists(): bool
    {
        return $this->csv_path !== null && Storage::disk('public')->exists($this->csv_path);
    }

    public function statusLabel(): string
    {
        return match ($this->status) {
            'completed' => 'Tamamlandı',
            'failed' => 'Hatalı',
            'processing' => 'İşleniyor',
            default => 'Taslak',
        };
    }

    public function periodLabel(): string
    {
        return match ($this->period_type) {
            'daily' => 'Günlük',
            'weekly' => 'Haftalık',
            'monthly' => 'Aylık',
            default => 'Özel Tarih',
        };
    }
}
