<?php

namespace App\Models;

use App\Services\PriorityService;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MarketingRequest extends Model
{
    use HasFactory;

    protected $fillable = [
        'request_number',
        'request_type_id',
        'submitted_by',
        'assigned_designer_id',
        'agent_id',
        'property_url',
        'property_address',
        'instructions',
        'required_at',
        'priority',
        'priority_override',
        'urgent_reason',
        'status',
        'campaign_details',
        'submitted_at',
        'started_at',
        'started_by',
        'first_proof_at',
        'latest_proof_at',
        'approved_at',
        'approved_by',
        'completed_at',
        'completed_by',
        'cancelled_at',
    ];

    protected $casts = [
        'required_at' => 'datetime',
        'submitted_at' => 'datetime',
        'started_at' => 'datetime',
        'first_proof_at' => 'datetime',
        'latest_proof_at' => 'datetime',
        'approved_at' => 'datetime',
        'completed_at' => 'datetime',
        'cancelled_at' => 'datetime',
        'priority_override' => 'boolean',
        'campaign_details' => 'array',
    ];

    public function requestType()
    {
        return $this->belongsTo(RequestType::class, 'request_type_id');
    }

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

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

    public function agent()
    {
        return $this->belongsTo(Agent::class, 'agent_id');
    }

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

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

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

    public function attachments()
    {
        return $this->hasMany(RequestAttachment::class, 'request_id');
    }

    public function versions()
    {
        return $this->hasMany(RequestVersion::class, 'request_id')->orderBy('version_number', 'asc');
    }

    public function latestVersion()
    {
        return $this->hasOne(RequestVersion::class, 'request_id')->latestOfMany('version_number');
    }

    public function revisions()
    {
        return $this->hasMany(RevisionRequest::class, 'request_id')->orderBy('revision_number', 'asc');
    }

    public function latestRevision()
    {
        return $this->hasOne(RevisionRequest::class, 'request_id')->latestOfMany('revision_number');
    }

    public function activityLogs()
    {
        return $this->hasMany(RequestActivityLog::class, 'request_id')->orderBy('id', 'asc');
    }

    public function getIsOverdueAttribute(): bool
    {
        if (in_array($this->status, ['COMPLETED', 'APPROVED', 'CANCELLED'])) {
            return false;
        }

        return Carbon::now()->greaterThan($this->required_at);
    }

    public function getCalculatedPriorityAttribute(): string
    {
        if ($this->priority_override) {
            return $this->priority;
        }

        return PriorityService::calculatePriority($this->required_at);
    }

    public function getRevisionsCountAttribute(): int
    {
        return $this->revisions()->count();
    }

    public function getVersionsCountAttribute(): int
    {
        return $this->versions()->count();
    }
}
