<?php

namespace App\Modules\Procurement\Services;

use App\Modules\Procurement\Contracts\ProcurementServiceInterface;
use App\Modules\Procurement\Models\PurchaseOrder;
use Illuminate\Support\Collection;

class ProcurementService implements ProcurementServiceInterface
{
    public function getPurchaseOrdersByDate(string $start, string $end, array $statuses = ['received', 'completed']): Collection
    {
        return PurchaseOrder::with(['vendor'])
            ->whereIn('status', $statuses)
            ->whereBetween('created_at', [$start . ' 00:00:00', $end . ' 23:59:59'])
            ->get();
    }

    public function getPurchaseOrdersByIds(array $ids, array $with = []): Collection
    {
        return PurchaseOrder::whereIn('id', $ids)->with($with)->get();
    }

    public function getDashboardStats(): array
    {
        return [
            'vendorsCount' => \App\Modules\Procurement\Models\Vendor::count(),
            'purchaseOrdersCount' => PurchaseOrder::count(),
            'pendingOrdersCount' => PurchaseOrder::where('status', 'pending')->count(),
        ];
    }

    public function updateReceivedQuantity(int $poItemId, float $quantity): void
    {
        $item = \App\Modules\Procurement\Models\PurchaseOrderItem::findOrFail($poItemId);
        $item->increment('received_quantity', $quantity);
        $item->refresh();

        // Check PO status
        $po = $item->purchaseOrder;
        
        // Load items to check completion
        $po->load('items');
        
        $allReceived = $po->items->every(function ($i) {
            return $i->received_quantity >= $i->quantity;
        });

        $anyReceived = $po->items->contains(function ($i) {
            return $i->received_quantity > 0;
        });

        if ($allReceived) {
            $po->status = 'completed'; // or 'received' based on workflow
        } elseif ($anyReceived) {
            $po->status = 'partial';
        }
        
        $po->save();
    }

    public function searchPendingPurchaseOrders(string $query): Collection
    {
        return PurchaseOrder::with(['vendor', 'items'])
            ->whereIn('status', ['approved', 'partial'])
            ->where(function($q) use ($query) {
                $q->where('code', 'like', "%{$query}%")
                  ->orWhereHas('vendor', function($vq) use ($query) {
                      $vq->where('name', 'like', "%{$query}%");
                  });
            })
            ->limit(20)
            ->get();
    }

    public function searchVendors(string $query): Collection
    {
        return \App\Modules\Procurement\Models\Vendor::where('name', 'like', "%{$query}%")
            ->orWhere('code', 'like', "%{$query}%")
            ->limit(20)
            ->get(['id', 'name', 'code']);
    }

    public function getPendingPurchaseOrders(): Collection
    {
        return PurchaseOrder::with(['vendor', 'items'])
            ->whereIn('status', ['approved', 'partial'])
            ->orderBy('created_at', 'desc')
            ->limit(50)
            ->get();
    }

    public function getAllVendors(): Collection
    {
        return \App\Modules\Procurement\Models\Vendor::orderBy('name')->get(['id', 'name', 'code']);
    }

    public function getVendorsByIds(array $ids): Collection
    {
        return \App\Modules\Procurement\Models\Vendor::whereIn('id', $ids)->get(['id', 'name', 'code']);
    }
}
