<?php

namespace App\Models;

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

class Project extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $guarded = [
        'id',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'sale_registration_enabled' => 'boolean',
        'images' => 'array',
        'budgets' => 'array',
        'requirements' => 'array',
        'attachments' => 'array',
        'start_date' => 'datetime',
        'end_date' => 'datetime',
        'deadline' => 'datetime',
        'presale_start' => 'date',
        'presale_end' => 'date',
        'sale_deactivation_at' => 'datetime',
        'unit_price' => 'decimal:0',
        'budget' => 'decimal:0',
        'estimated_profit' => 'decimal:0',
    ];

    protected $appends = [];
    protected $with = [];

    public const STATUS_PENDING = 'pending';
    public const STATUS_ACTIVE = 'active';
    public const STATUS_APPROVED = 'approved';
    public const STATUS_REJECTED = 'rejected';
    public const STATUS_EDIT = 'edit';
    public const STATUS_WAITING = 'waiting';
    public const STATUS_ARBITRATION = 'arbitration';
    public const STATUS_UNSIGNED = 'unsigned';
    public const STATUS_COMPLETED = 'completed';
    public const STATUS_CANCELLED = 'cancelled';
    public const STATUS_EXPIRED = 'expired';

    public const UNIT_HEAD = 'head';
    public const UNIT_COUNT = 'count';
    public const UNIT_WEIGHT = 'weight';

    public static function statusOptions(): array
    {
        return [
            self::STATUS_PENDING => 'در انتظار',
            self::STATUS_ACTIVE => 'فعال',
            self::STATUS_APPROVED => 'تأیید شده',
            self::STATUS_REJECTED => 'رد شده',
            self::STATUS_EDIT => 'در حال ویرایش',
            self::STATUS_WAITING => 'در انتظار تأیید',
            self::STATUS_ARBITRATION => 'داوری',
            self::STATUS_UNSIGNED => 'امضا نشده',
            self::STATUS_COMPLETED => 'تکمیل شده',
            self::STATUS_CANCELLED => 'لغو شده',
            self::STATUS_EXPIRED => 'منقضی شده',
        ];
    }

    public static function unitOptions(): array
    {
        return [
            self::UNIT_HEAD => 'راس',
            self::UNIT_COUNT => 'تعداد',
            self::UNIT_WEIGHT => 'وزن',
        ];
    }

    /**
     * @return list<string>
     */
    public static function apiEagerLoads(): array
    {
        return [
            'user.producer',
            'user.expert',
            'user.roles',
            'producer.user.roles',
            'producer.user.producer',
            'producer.user.expert',
            'category',
            'product',
            'breed',
        ];
    }

    public function scopeWithApiRelations(Builder $query): Builder
    {
        return $query->with(self::apiEagerLoads());
    }

    public function loadApiRelations(): self
    {
        $this->load(self::apiEagerLoads());

        return $this;
    }

    protected static function boot(): void
    {
        parent::boot();
        static::addGlobalScope('order', function (Builder $builder) {
            $builder->orderByDesc('created_at');
        });
        static::creating(function (Project $project): void {
            if (empty($project->code)) {
                $project->code = (int) static::withTrashed()->max('code') + 1;
            }
        });
    }

    /*
     * Relations
     */
    public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function updatedBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function producer(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Producer::class);
    }

    public function category(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Category::class);
    }

    public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    public function breed(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Breed::class);
    }

    public function province(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(\Mortezaa97\Regions\Models\Province::class);
    }

    public function county(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(\Mortezaa97\Regions\Models\County::class);
    }

    public function orders(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(Order::class);
    }

    public function offers(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(Offer::class);
    }

    public function reports(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(Report::class);
    }

    public function invoices(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(Invoice::class);
    }

    public function walletTransactions(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(WalletTransaction::class);
    }

    public function mediaItems(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(MediaItem::class);
    }

    public function payments(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(Payment::class);
    }

    public function contractSignatures(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(ProjectContractSignature::class);
    }

    public function completion(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        return $this->hasOne(ProjectCompletion::class);
    }

    public function projectRequest(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(ProjectRequest::class, 'project_request_id');
    }
}

