<?php

namespace App\Models;

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

/**
 * @package App\Models
 * @property integer $id
 * @property integer $order_id
 * @property string $type
 * @property string $path
 *
 * @property-read Order|BelongsTo $order
 *
 * @mother isContract(): bool
 * @mother isMasterContract(): bool
 */
class OrderContract extends Model
{
    use HasFactory;

    const TYPE_CONTRACT = 'contract';
    const TYPE_MASTER = 'master_contract';

    protected $guarded = ['id'];

    protected $casts = [
        'order_id' => 'integer',
    ];

    public function getPath()
    {
        if (!empty($this->path)) {
            if (in_array(env('FILESYSTEM_DISK'), ['s3', 'minio'])) {
                return Storage::temporaryUrl(
                    $this->path,
                    Date::now()->addMinutes(5)
                );
            }

            return env('APP_URL') . '/storage/' . $this->path;
        }

        return null;
    }

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

    public function isContract(): bool
    {
        return $this->type === self::TYPE_CONTRACT;
    }

    public function isMasterContract(): bool
    {
        return $this->type === self::TYPE_MASTER;
    }
}
