<?php

namespace App\Http\Controllers;

use App\Http\Requests\StorePurchaseRequest;
use App\Models\Party;
use App\Models\Yarn;
use App\Models\Purchase;
use App\Services\PurchaseService;
use App\Services\ActivityLogService;
use Illuminate\Http\Request;

class PurchaseController extends Controller
{
    public function __construct(
        protected PurchaseService $purchaseService,
        protected ActivityLogService $log
    ) {
    }

    public function index(Request $request)
    {
        $purchases = $this->purchaseService->getFiltered($request);
        $totals = $this->purchaseService->getTotals($purchases);

        return view('purchase.index', compact('purchases', 'totals'));
    }

    public function create()
    {
        $suppliers = Party::suppliers()->active()->orderBy('name')->get();
        $yarns = Yarn::active()->orderBy('name')->get();
        $items = collect([]);

        return view('purchase.create', compact('suppliers', 'yarns', 'items'));
    }

    public function store(StorePurchaseRequest $request)
    {
        $purchase = $this->purchaseService->create($request->validated());
        $this->log->log('created', 'purchase', $purchase, "Purchase {$purchase->voucher_no} created");

        return redirect()
            ->route('purchase.show', $purchase->id)
            ->with('success', "Purchase {$purchase->voucher_no} created successfully.");
    }

    public function show(Purchase $purchase)
    {
        $purchase->load(['party', 'items.yarn', 'creator']);

        return view('purchase.show', compact('purchase'));
    }

    public function edit(Purchase $purchase)
    {
        $purchase->load(['party', 'items.yarn']);
        $suppliers = Party::suppliers()->active()->orderBy('name')->get();
        $yarns = Yarn::active()->orderBy('name')->get();
        $items = $purchase->items->map(fn ($i) => [
            'yarn_id' => $i->yarn_id,
            'lot_no'  => $i->lot_no ?? '',
            'bags'    => (int) $i->bags,
            'qty'     => (float) $i->quantity,
            'rate'    => (float) $i->rate,
            'amount'  => (float) $i->amount,
        ]);

        return view('purchase.edit', compact('purchase', 'suppliers', 'yarns', 'items'));
    }

    public function update(StorePurchaseRequest $request, Purchase $purchase)
    {
        $old = $purchase->toArray();
        $this->purchaseService->update($purchase, $request->validated());
        $this->log->log('updated', 'purchase', $purchase, "Purchase {$purchase->voucher_no} updated", $old, $purchase->fresh()->toArray());

        return redirect()
            ->route('purchase.show', $purchase->id)
            ->with('success', "Purchase {$purchase->voucher_no} updated successfully.");
    }

    public function destroy(Purchase $purchase)
    {
        $vno = $purchase->voucher_no;
        $this->purchaseService->delete($purchase);
        $this->log->logAction('deleted', 'purchase', Purchase::class, $purchase->id, "Purchase {$vno} deleted");

        return redirect()
            ->route('purchase.index')
            ->with('success', 'Purchase deleted successfully.');
    }
}
