<?php

namespace App\Modules\Finance\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Finance\Models\AccountPayable;
use Illuminate\Http\Request;
use Inertia\Inertia;

use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;

class AccountPayableController extends Controller
{
    protected $procurementService;
    protected $inventoryService;

    public function __construct(
        ProcurementServiceInterface $procurementService,
        InventoryServiceInterface $inventoryService
    ) {
        $this->procurementService = $procurementService;
        $this->inventoryService = $inventoryService;
    }
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $query = AccountPayable::with(['creator']);

        // 關鍵字搜尋 (單號、供應商名稱)
        if ($request->filled('search')) {
            $search = $request->search;
            
            // 透過 ProcurementService 查詢符合關鍵字的 Vendor IDs
            $matchedVendors = $this->procurementService->searchVendors($search);
            $vendorIds = $matchedVendors->pluck('id')->toArray();
            
            $query->where(function ($q) use ($search, $vendorIds) {
                $q->where('document_number', 'like', "%{$search}%");
                if (!empty($vendorIds)) {
                    $q->orWhereIn('vendor_id', $vendorIds);
                }
            });
        }

        // 狀態過濾
        if ($request->filled('status') && $request->status !== 'all') {
            $query->where('status', $request->status);
        }
        
        // 供應商過濾
        if ($request->filled('vendor_id') && $request->vendor_id !== 'all') {
            $query->where('vendor_id', $request->vendor_id);
        }

        // 日期區間過濾
        if ($request->filled('date_start')) {
            $query->where('due_date', '>=', $request->date_start);
        }
        if ($request->filled('date_end')) {
            $query->where('due_date', '<=', $request->date_end);
        }

        $defaultPerPage = \App\Modules\Core\Models\SystemSetting::getVal('display.per_page', 10);
        $perPage = $request->input('per_page', $defaultPerPage);
        if (!in_array((int)$perPage, [10, 20, 50, 100])) {
            $perPage = $defaultPerPage;
        }
        $payables = $query->latest()->paginate($perPage)->withQueryString();

        // Manual Hydration for Vendors
        $allVendorIds = collect($payables->items())->pluck('vendor_id')->unique()->filter()->toArray();
        $vendorsMap = $this->procurementService->getVendorsByIds($allVendorIds)->keyBy('id');
        
        $payables->getCollection()->transform(function ($item) use ($vendorsMap) {
            $item->vendor = $vendorsMap->get($item->vendor_id);
            return $item;
        });

        $vendors = $this->procurementService->getAllVendors();

        return Inertia::render('AccountPayable/Index', [
            'payables' => $payables,
            'filters' => $request->all(['search', 'status', 'vendor_id', 'date_start', 'date_end', 'per_page']),
            'vendors' => $vendors,
        ]);
    }

    /**
     * Display the specified resource.
     */
    public function show(AccountPayable $accountPayable)
    {
        $accountPayable->load(['creator']);
        
        if ($accountPayable->vendor_id) {
            $accountPayable->vendor = $this->procurementService->getVendorsByIds([$accountPayable->vendor_id])->first();
        }

        // 嘗試加載來源單據資訊 (目前支援 goods_receipt)
        $sourceDocumentCode = null;
        if ($accountPayable->source_document_type === 'goods_receipt') {
            $receiptData = app(\App\Modules\Inventory\Contracts\GoodsReceiptServiceInterface::class)
                ->getGoodsReceiptData($accountPayable->source_document_id);
            if ($receiptData) {
                $sourceDocumentCode = $receiptData['code'] ?? null;
            }
        }

        return Inertia::render('AccountPayable/Show', [
            // 將 model 轉換成 array 加入額外資訊
            'payable' => array_merge($accountPayable->toArray(), ['source_document_code' => $sourceDocumentCode]),
        ]);
    }

    /**
     * 更新發票資訊
     */
    public function updateInvoice(Request $request, AccountPayable $accountPayable)
    {
        $validated = $request->validate([
            'invoice_number' => 'nullable|string|max:50',
            'invoice_date' => 'nullable|date',
        ]);

        $accountPayable->update([
            'invoice_number' => $validated['invoice_number'],
            'invoice_date' => $validated['invoice_date'],
        ]);

        return back()->with('success', '發票資訊已更新');
    }

    /**
     * 標記已付款
     */
    public function pay(Request $request, AccountPayable $accountPayable)
    {
        $validated = $request->validate([
            'payment_method' => 'required|string|max:50',
            'paid_at' => 'required|date',
            'payment_note' => 'nullable|string|max:255',
        ]);

        if ($accountPayable->status === AccountPayable::STATUS_PAID) {
            return back()->with('error', '該帳款已經標記為已付款');
        }

        $accountPayable->update([
            'status' => AccountPayable::STATUS_PAID,
            'payment_method' => $validated['payment_method'],
            'paid_at' => $validated['paid_at'],
            'payment_note' => $validated['payment_note'],
        ]);

        return back()->with('success', '帳款已成功標記為已付款');
    }
}
