<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ComplianceReport extends Model
{
    use HasFactory;

    protected $fillable = [
        'report_type',
        'report_code',
        'title',
        'description',
        'report_date',
        'period_start',
        'period_end',
        'report_data',
        'metadata',
        'filters_applied',
        'status',
        'review_notes',
        'submitted_at',
        'approved_at',
        'is_regulatory_required',
        'due_date',
        'regulatory_body',
        'submission_reference',
        'file_path',
        'file_name',
        'file_mime_type',
        'file_size',
        'created_by',
        'reviewed_by',
        'approved_by',
    ];

    protected $casts = [
        'report_date' => 'date',
        'period_start' => 'date',
        'period_end' => 'date',
        'submitted_at' => 'datetime',
        'approved_at' => 'datetime',
        'due_date' => 'date',
        'report_data' => 'array',
        'metadata' => 'array',
        'filters_applied' => 'array',
        'is_regulatory_required' => 'boolean',
    ];

    // Relationships
    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function reviewer()
    {
        return $this->belongsTo(User::class, 'reviewed_by');
    }

    public function approver()
    {
        return $this->belongsTo(User::class, 'approved_by');
    }

    // Scopes
    public function scopePendingReview($query)
    {
        return $query->where('status', 'pending_review');
    }

    public function scopeApproved($query)
    {
        return $query->where('status', 'approved');
    }

    public function scopeSubmitted($query)
    {
        return $query->where('status', 'submitted');
    }

    public function scopeRegulatoryRequired($query)
    {
        return $query->where('is_regulatory_required', true);
    }

    public function scopeByType($query, $type)
    {
        return $query->where('report_type', $type);
    }

    public function scopeOverdue($query)
    {
        return $query->where('due_date', '<', now())
                    ->where('status', '!=', 'submitted');
    }

    // Accessors
    public function getIsOverdueAttribute()
    {
        return $this->due_date && $this->due_date < now() && $this->status !== 'submitted';
    }

    public function getDaysToDueAttribute()
    {
        return $this->due_date ? now()->diffInDays($this->due_date, false) : null;
    }

    public function getFileUrlAttribute()
    {
        return $this->file_path ? asset('storage/' . $this->file_path) : null;
    }

    // Methods
    public function markAsSubmitted($reference = null)
    {
        $this->update([
            'status' => 'submitted',
            'submitted_at' => now(),
            'submission_reference' => $reference,
        ]);
    }

    public function approve($approverId)
    {
        $this->update([
            'status' => 'approved',
            'approved_by' => $approverId,
            'approved_at' => now(),
        ]);
    }

    public function reject($reviewerId, $notes = null)
    {
        $this->update([
            'status' => 'rejected',
            'reviewed_by' => $reviewerId,
            'review_notes' => $notes,
        ]);
    }

    public function canBeEdited()
    {
        return in_array($this->status, ['draft', 'pending_review', 'rejected']);
    }

    public function isEditable()
    {
        return $this->canBeEdited();
    }

    public function getStatusColor()
    {
        return match($this->status) {
            'draft' => 'gray',
            'pending_review' => 'yellow',
            'approved' => 'green',
            'submitted' => 'blue',
            'rejected' => 'red',
            default => 'gray',
        };
    }
}
