<?php

namespace App\Models\BadDebts;

use App\Models\Members\User;
use App\Models\Orders\Client;
use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class BadDebt extends Model
{
    use Auditable, BelongsToTenant, SoftDeletes;

    protected $fillable = [
        'tenant_id',
        'client_id',
        'debt_amount',
        'status',
        'overdue_days',
        'assigned_user_id',
        'occurred_at',
        'closed_at',
        'is_active',
        'options',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'debt_amount' => 'decimal:2',
        'overdue_days' => 'integer',
        'is_active' => 'boolean',
        'options' => 'array',
        'occurred_at' => 'date',
        'closed_at' => 'date',
    ];

    /**
     * 상태 상수
     */
    public const STATUS_COLLECTING = 'collecting';

    public const STATUS_LEGAL_ACTION = 'legal_action';

    public const STATUS_RECOVERED = 'recovered';

    public const STATUS_BAD_DEBT = 'bad_debt';

    /**
     * 상태 목록
     */
    public const STATUSES = [
        self::STATUS_COLLECTING => '추심중',
        self::STATUS_LEGAL_ACTION => '법적조치',
        self::STATUS_RECOVERED => '회수완료',
        self::STATUS_BAD_DEBT => '대손처리',
    ];

    /**
     * 거래처 관계
     */
    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class);
    }

    /**
     * 담당자 관계
     */
    public function assignedUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_user_id');
    }

    /**
     * 생성자 관계
     */
    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * 서류 관계
     */
    public function documents(): HasMany
    {
        return $this->hasMany(BadDebtDocument::class);
    }

    /**
     * 메모 관계
     */
    public function memos(): HasMany
    {
        return $this->hasMany(BadDebtMemo::class)->orderBy('created_at', 'desc');
    }

    /**
     * 상태 라벨 속성
     */
    public function getStatusLabelAttribute(): string
    {
        return self::STATUSES[$this->status] ?? $this->status;
    }

    /**
     * 활성 스코프
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * 상태별 스코프
     */
    public function scopeStatus($query, string $status)
    {
        return $query->where('status', $status);
    }

    /**
     * 추심중 스코프
     */
    public function scopeCollecting($query)
    {
        return $query->where('status', self::STATUS_COLLECTING);
    }

    /**
     * 법적조치 스코프
     */
    public function scopeLegalAction($query)
    {
        return $query->where('status', self::STATUS_LEGAL_ACTION);
    }

    /**
     * 회수완료 스코프
     */
    public function scopeRecovered($query)
    {
        return $query->where('status', self::STATUS_RECOVERED);
    }

    /**
     * 대손처리 스코프
     */
    public function scopeBadDebt($query)
    {
        return $query->where('status', self::STATUS_BAD_DEBT);
    }
}
