<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class ReportSchedule extends Model
{
    use HasFactory;

    protected $fillable = [
        'client_id', 'report_template_id', 'frequency', 'daily_time', 'weekly_day', 'monthly_day',
        'timezone', 'channels', 'recipients', 'cc', 'bcc', 'include_pdf', 'include_excel', 'include_csv', 'is_active',
        'last_run_at', 'next_run_at', 'last_status', 'last_error',
    ];

    protected $casts = [
        'channels' => 'array',
        'recipients' => 'array',
        'cc' => 'array',
        'bcc' => 'array',
        'include_pdf' => 'boolean',
        'include_excel' => 'boolean',
        'include_csv' => 'boolean',
        'is_active' => 'boolean',
        'last_run_at' => 'datetime',
        'next_run_at' => 'datetime',
    ];

    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class);
    }

    public function template(): BelongsTo
    {
        return $this->belongsTo(ReportTemplate::class, 'report_template_id');
    }

    public function generatedReports(): HasMany
    {
        return $this->hasMany(GeneratedReport::class, 'report_schedule_id');
    }
}
