<?php

namespace App\Models\Sales;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class BusinessCardRequest extends Model
{
    public const STATUS_PENDING = 'pending';

    public const STATUS_ORDERED = 'ordered';

    public const STATUS_PROCESSED = 'processed';

    public const STATUS_LABELS = [
        self::STATUS_PENDING => '요청',
        self::STATUS_ORDERED => '제작의뢰',
        self::STATUS_PROCESSED => '처리완료',
    ];

    protected $fillable = [
        'tenant_id',
        'user_id',
        'name',
        'phone',
        'title',
        'email',
        'quantity',
        'memo',
        'status',
        'ordered_by',
        'ordered_at',
        'processed_by',
        'processed_at',
        'process_memo',
    ];

    protected $casts = [
        'ordered_at' => 'datetime',
        'processed_at' => 'datetime',
        'quantity' => 'integer',
    ];

    protected $attributes = [
        'status' => self::STATUS_PENDING,
        'quantity' => 100,
    ];

    // =========================================================================
    // 관계
    // =========================================================================

    public function requester(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function orderer(): BelongsTo
    {
        return $this->belongsTo(User::class, 'ordered_by');
    }

    public function processor(): BelongsTo
    {
        return $this->belongsTo(User::class, 'processed_by');
    }

    // =========================================================================
    // 스코프
    // =========================================================================

    public function scopePending($query)
    {
        return $query->where('status', self::STATUS_PENDING);
    }

    public function scopeOrdered($query)
    {
        return $query->where('status', self::STATUS_ORDERED);
    }

    public function scopeProcessed($query)
    {
        return $query->where('status', self::STATUS_PROCESSED);
    }

    public function scopeOfTenant($query, int $tenantId)
    {
        return $query->where('tenant_id', $tenantId);
    }

    // =========================================================================
    // 접근자
    // =========================================================================

    public function getStatusLabelAttribute(): string
    {
        return self::STATUS_LABELS[$this->status] ?? $this->status;
    }

    public function getIsPendingAttribute(): bool
    {
        return $this->status === self::STATUS_PENDING;
    }

    public function getIsOrderedAttribute(): bool
    {
        return $this->status === self::STATUS_ORDERED;
    }

    // =========================================================================
    // 헬퍼 메서드
    // =========================================================================

    public function markAsOrdered(int $orderedBy): bool
    {
        if (! $this->is_pending) {
            return false;
        }

        $this->status = self::STATUS_ORDERED;
        $this->ordered_by = $orderedBy;
        $this->ordered_at = now();

        return $this->save();
    }

    public function markAsProcessed(int $processedBy, ?string $memo = null): bool
    {
        if (! $this->is_ordered) {
            return false;
        }

        $this->status = self::STATUS_PROCESSED;
        $this->processed_by = $processedBy;
        $this->processed_at = now();
        $this->process_memo = $memo;

        return $this->save();
    }
}
