<?php

namespace App\Repositories\Eloquent;

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

class BranchProductRepository extends BaseRepository
{
    protected function resolveModel(): Model
    {
        return new BranchProduct();
    }

    public function getByBranch(int $branchId)
    {
        return $this->model->forBranch($branchId)
            ->with('product.category')
            ->get();
    }

    public function getAvailableByBranch(int $branchId)
    {
        return $this->model->forBranch($branchId)
            ->available()
            ->with('product.category')
            ->get();
    }

    public function findByBranchAndProduct(int $branchId, int $productId): ?BranchProduct
    {
        return $this->model->where('branch_id', $branchId)
            ->where('product_id', $productId)
            ->first();
    }

    public function updateOrCreateForBranch(int $branchId, int $productId, array $data): BranchProduct
    {
        return $this->model->updateOrCreate(
            ['branch_id' => $branchId, 'product_id' => $productId],
            $data
        );
    }

    public function bulkUpdateAvailability(int $branchId, array $productIds, bool $isAvailable): int
    {
        return $this->model->forBranch($branchId)
            ->whereIn('product_id', $productIds)
            ->update(['is_available' => $isAvailable]);
    }
}
