<?php

namespace App\Models\Tenants;

use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;

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

    protected $fillable = [
        'tenant_id',
        'expected_payment_date',
        'settlement_date',
        'transaction_type',
        'amount',
        'client_id',
        'client_name',
        'bank_account_id',
        'account_code',
        'payment_status',
        'approval_status',
        'description',
        'source_type',
        'source_id',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    protected $casts = [
        'expected_payment_date' => 'date',
        'settlement_date' => 'date',
        'amount' => 'decimal:2',
        'client_id' => 'integer',
        'bank_account_id' => 'integer',
    ];

    /**
     * 거래유형 목록
     */
    public const TRANSACTION_TYPES = [
        'purchase' => '매입',
        'card' => '카드결제',
        'bill' => '발행어음',
        'advance' => '선급금',
        'suspense' => '가지급금',
        'rent' => '임대료',
        'salary' => '급여',
        'insurance' => '보험료',
        'tax' => '세금',
        'utilities' => '공과금',
        'other' => '기타',
    ];

    /**
     * 지급상태 목록
     */
    public const PAYMENT_STATUSES = [
        'pending' => '미지급',
        'partial' => '부분지급',
        'paid' => '지급완료',
        'overdue' => '연체',
    ];

    /**
     * 결재상태 목록
     */
    public const APPROVAL_STATUSES = [
        'none' => '미신청',
        'pending' => '결재대기',
        'approved' => '결재완료',
        'rejected' => '반려',
    ];

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

    /**
     * 계좌 관계
     */
    public function bankAccount(): BelongsTo
    {
        return $this->belongsTo(BankAccount::class);
    }

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

    /**
     * 거래처명 조회 (회원/비회원 통합)
     */
    public function getDisplayClientNameAttribute(): string
    {
        if ($this->client) {
            return $this->client->name;
        }

        return $this->client_name ?? '';
    }

    /**
     * 거래유형 라벨
     */
    public function getTransactionTypeLabelAttribute(): string
    {
        return self::TRANSACTION_TYPES[$this->transaction_type] ?? $this->transaction_type;
    }

    /**
     * 지급상태 라벨
     */
    public function getPaymentStatusLabelAttribute(): string
    {
        return self::PAYMENT_STATUSES[$this->payment_status] ?? $this->payment_status;
    }

    /**
     * 결재상태 라벨
     */
    public function getApprovalStatusLabelAttribute(): string
    {
        return self::APPROVAL_STATUSES[$this->approval_status] ?? $this->approval_status;
    }

    /**
     * 원본 소스 관계 (Polymorphic)
     */
    public function source(): MorphTo
    {
        return $this->morphTo();
    }

    /**
     * 원본 소스로 검색
     */
    public function scopeBySource(Builder $query, string $sourceType, int $sourceId): Builder
    {
        return $query->where('source_type', $sourceType)
            ->where('source_id', $sourceId);
    }

    /**
     * 동기화된 레코드 여부
     */
    public function isSynced(): bool
    {
        return ! is_null($this->source_type) && ! is_null($this->source_id);
    }
}
