<?php

namespace App\Observers\ExpenseSync;

use App\Models\Tenants\ExpectedExpense;
use App\Models\Tenants\Purchase;

/**
 * 매입 데이터를 예상 지출(expected_expenses)로 자동 동기화
 */
class PurchaseExpenseSyncObserver
{
    /**
     * 매입 생성 시 → 예상 지출 생성
     */
    public function created(Purchase $purchase): void
    {
        $this->syncToExpectedExpense($purchase);
    }

    /**
     * 매입 수정 시 → 예상 지출 업데이트
     */
    public function updated(Purchase $purchase): void
    {
        $this->syncToExpectedExpense($purchase);
    }

    /**
     * 매입 삭제 시 → 예상 지출 삭제
     */
    public function deleted(Purchase $purchase): void
    {
        ExpectedExpense::withoutGlobalScopes()
            ->bySource('purchases', $purchase->id)
            ->delete();
    }

    /**
     * 매입 복원 시 → 예상 지출 복원
     */
    public function restored(Purchase $purchase): void
    {
        // 기존 soft deleted 레코드가 있으면 복원, 없으면 생성
        $expense = ExpectedExpense::withTrashed()
            ->withoutGlobalScopes()
            ->bySource('purchases', $purchase->id)
            ->first();

        if ($expense) {
            $expense->restore();
            $this->updateExpectedExpense($expense, $purchase);
        } else {
            $this->syncToExpectedExpense($purchase);
        }
    }

    /**
     * 매입 강제 삭제 시 → 예상 지출 강제 삭제
     */
    public function forceDeleted(Purchase $purchase): void
    {
        ExpectedExpense::withTrashed()
            ->withoutGlobalScopes()
            ->bySource('purchases', $purchase->id)
            ->forceDelete();
    }

    /**
     * 예상 지출에 동기화
     */
    protected function syncToExpectedExpense(Purchase $purchase): void
    {
        $expense = ExpectedExpense::withoutGlobalScopes()
            ->bySource('purchases', $purchase->id)
            ->first();

        if ($expense) {
            $this->updateExpectedExpense($expense, $purchase);
        } else {
            $this->createExpectedExpense($purchase);
        }
    }

    /**
     * 예상 지출 생성
     */
    protected function createExpectedExpense(Purchase $purchase): void
    {
        ExpectedExpense::create([
            'tenant_id' => $purchase->tenant_id,
            'expected_payment_date' => $purchase->purchase_date,
            'transaction_type' => 'purchase',
            'amount' => $purchase->total_amount,
            'client_id' => $purchase->client_id,
            'description' => $purchase->description ?? "매입번호: {$purchase->purchase_number}",
            'payment_status' => $purchase->withdrawal_id ? 'paid' : 'pending',
            'approval_status' => 'none',
            'source_type' => 'purchases',
            'source_id' => $purchase->id,
            'created_by' => $purchase->created_by,
            'updated_by' => $purchase->updated_by,
        ]);
    }

    /**
     * 예상 지출 업데이트
     */
    protected function updateExpectedExpense(ExpectedExpense $expense, Purchase $purchase): void
    {
        $expense->update([
            'expected_payment_date' => $purchase->purchase_date,
            'amount' => $purchase->total_amount,
            'client_id' => $purchase->client_id,
            'description' => $purchase->description ?? "매입번호: {$purchase->purchase_number}",
            'payment_status' => $purchase->withdrawal_id ? 'paid' : 'pending',
            'updated_by' => $purchase->updated_by,
        ]);
    }
}
