<?php

namespace App\Domains\Catalog\Controllers;

use App\Domains\Catalog\Models\Product;
use App\Domains\Catalog\Models\ProductVariant;
use App\Domains\Catalog\Requests\StoreProductVariantRequest;
use App\Domains\Catalog\Requests\UpdateProductVariantRequest;
use App\Domains\Catalog\Resources\ProductVariantResource;
use App\Domains\Catalog\Services\ProductService;
use App\Domains\Tenant\Models\Tenant;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class ProductVariantController extends Controller
{
    public function index(Tenant $tenant, Product $producto): JsonResponse
    {

        $query = ProductVariant::query()
            ->where('producto_id', $producto->id)
            ->with(['product', 'definitions.attribute.options', 'attachments'])
            ->latest();

        return ProductVariantResource::collection($query->paginateFromRequest())->response();
    }

    public function store(StoreProductVariantRequest $request, Tenant $tenant, Product $producto, ProductService $productService): JsonResponse
    {
        $producto = $this->resolveScopedProduct($tenant, $producto);

        $variant = $productService->createVariant($producto, $request->validated());

        return ProductVariantResource::make($variant)->response()->setStatusCode(201);
    }

    public function show(Tenant $tenant, Product $producto, ProductVariant $productVariant): ProductVariantResource
    {
        $producto = $this->resolveScopedProduct($tenant, $producto);
        $productVariant = $this->resolveScopedVariant($producto, $productVariant);

        return ProductVariantResource::make($productVariant->load(['product', 'definitions.attribute.options', 'attachments']));
    }

    public function update(UpdateProductVariantRequest $request, Tenant $tenant, Product $producto, ProductVariant $productVariant, ProductService $productService): ProductVariantResource
    {
        $producto = $this->resolveScopedProduct($tenant, $producto);
        $productVariant = $this->resolveScopedVariant($producto, $productVariant);

        $productVariant = $productService->updateVariant($productVariant, $request->validated());

        return ProductVariantResource::make($productVariant);
    }

    public function destroy(Tenant $tenant, Product $producto, ProductVariant $productVariant, ProductService $productService): Response
    {
        $producto = $this->resolveScopedProduct($tenant, $producto);
        $productVariant = $this->resolveScopedVariant($producto, $productVariant);
        $productService->deleteVariant($productVariant);

        return response()->noContent();
    }

    protected function resolveScopedProduct(Tenant $tenant, Product $product): Product
    {
        if ($product->tenant_codigo !== $tenant->codigo) {
            throw new NotFoundHttpException('Product not found for tenant.');
        }

        return $product;
    }

    protected function resolveScopedVariant(Product $product, ProductVariant $variant): ProductVariant
    {
        if ($variant->producto_id !== $product->id) {
            throw new NotFoundHttpException('Product variant not found for product.');
        }

        return $variant;
    }
}
