<?php

namespace App\Repositories\Eloquent;

use App\Models\PurchaseOrder;
use Illuminate\Database\Eloquent\Model;

class PurchaseOrderRepository extends BaseRepository
{
    protected function resolveModel(): Model
    {
        return new PurchaseOrder();
    }

    public function getForBranch(int $branchId)
    {
        return $this->model->forBranch($branchId)
            ->with(['user', 'items.ingredient'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getByStatus(int $branchId, string $status)
    {
        return $this->model->forBranch($branchId)
            ->byStatus($status)
            ->with(['user', 'items.ingredient'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getPending(int $branchId)
    {
        return $this->model->forBranch($branchId)
            ->pending()
            ->with(['user', 'items.ingredient'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function findWithItems(int $id): ?PurchaseOrder
    {
        return $this->model->with(['branch', 'user', 'items.ingredient'])->find($id);
    }

    public function findByPoNumber(string $poNumber): ?PurchaseOrder
    {
        return $this->model->where('po_number', $poNumber)->first();
    }
}
