<?php

namespace App\Repositories\Eloquent;

use App\Models\ProductionBatch;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class ProductionBatchRepository extends BaseRepository
{
    protected function resolveModel(): Model
    {
        return new ProductionBatch();
    }

    public function getAll(array $filters = []): Collection
    {
        $query = $this->model->with(['branch', 'product', 'recipe', 'producer']);

        if (!empty($filters['branch_id'])) {
            $query->forBranch($filters['branch_id']);
        }

        if (!empty($filters['product_id'])) {
            $query->forProduct($filters['product_id']);
        }

        if (!empty($filters['status'])) {
            $query->where('status', $filters['status']);
        }

        if (!empty($filters['date'])) {
            $query->whereDate('produced_at', $filters['date']);
        }

        if (!empty($filters['start_date']) && !empty($filters['end_date'])) {
            $query->whereBetween('produced_at', [$filters['start_date'], $filters['end_date']]);
        }

        return $query->orderBy('produced_at', 'desc')->get();
    }

    public function getActiveBatches(int $branchId, ?int $productId = null): Collection
    {
        $query = $this->model
            ->forBranch($branchId)
            ->active()
            ->notExpired()
            ->hasRemainingScoops()
            ->with(['product', 'recipe']);

        if ($productId) {
            $query->forProduct($productId);
        }

        return $query->orderBy('produced_at', 'asc')->get();
    }

    public function getWithDetails(int $id): ?ProductionBatch
    {
        return $this->model
            ->with(['branch', 'product', 'recipe', 'producer', 'ingredients.ingredient', 'scoopSales'])
            ->find($id);
    }

    public function findActiveBatchForProduct(int $branchId, int $productId): ?ProductionBatch
    {
        return $this->model
            ->forBranch($branchId)
            ->forProduct($productId)
            ->active()
            ->notExpired()
            ->hasRemainingScoops()
            ->orderBy('produced_at', 'asc')
            ->first();
    }

    public function getExpiredBatches(): Collection
    {
        return $this->model
            ->where('status', 'active')
            ->where('expires_at', '<', now())
            ->get();
    }

    public function getDailyScoopSummary(int $branchId, string $date): Collection
    {
        return $this->model
            ->forBranch($branchId)
            ->whereDate('produced_at', $date)
            ->with('product')
            ->get()
            ->map(function ($batch) {
                return [
                    'batch_id' => $batch->id,
                    'batch_number' => $batch->batch_number,
                    'product_name' => $batch->product->name,
                    'total_scoops' => $batch->total_scoops,
                    'sold_scoops' => $batch->sold_scoops,
                    'remaining_scoops' => $batch->remaining_scoops,
                    'wasted_scoops' => $batch->wasted_scoops,
                    'usage_percentage' => $batch->usage_percentage,
                    'status' => $batch->status,
                ];
            });
    }
}
