<?php

namespace Modules\Inventory\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Modules\Core\Helpers\ApiResponse;
use Modules\Core\Http\Requests\UpdateStatusRequest;
use Modules\Inventory\DTO\ProductListFilter;
use Modules\Inventory\Http\Requests\Product\PreviewProductAccountsRequest;
use Modules\Inventory\Http\Requests\Product\StoreProductRequest;
use Modules\Inventory\Http\Requests\Product\UpdateProductRequest;
use Modules\Inventory\Models\Product;
use Modules\Inventory\Services\ProductService;
use Modules\Inventory\Transformers\ProductResource;

class ProductController extends Controller
{
    use AuthorizesRequests;

    public function __construct(
        protected ProductService $productService
    ){}

    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $this->authorize('viewAny', Product::class);

        $filter = ProductListFilter::fromRequest($request);
        $data = $this->productService->list($filter);

        return (ProductResource::collection($data))->additional([
            'status' => true,
            'message' => 'Products retrieved successfully.'
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreProductRequest $request)
    {
        $this->authorize('create', Product::class);

        $product = $this->productService->store($request->validated());

        return (new ProductResource($product))->additional([
            'status' => true,
            'message' => 'Product created successfully.'
        ]);
    }

    /**
     * Show the specified resource.
     */
    public function show(int $id)
    {
        $this->authorize('viewAny', Product::class);

        $data = $this->productService->show($id);

        return (new ProductResource($data))->additional([
            'status' => true,
            'message' => 'Product retrieved successfully.'
        ]);
    }


    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateProductRequest $request, Product $product)
    {
        $this->authorize('update', $product);

        $product = $this->productService->update(
            $product,
            $request->validated()
        );

        return (new ProductResource($product))->additional([
            'status' => true,
            'message' => 'Product updated successfully.'
        ]);
    }

    public function updateStatus(UpdateStatusRequest $request, Product $product)
    {
        $this->authorize('updateStatus', $product);

        $this->productService->updateStatus(
            $product,
            $request->validated()
        );
        return ApiResponse::successResponse('Product status updated successfully.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id) {}

    public function previewAccounts(PreviewProductAccountsRequest $request)
    {
        $preview = $this->productService->previewAccounts($request->validated());

        return ApiResponse::successResponse(
            message: 'Accounting preview generated successfully.',
            data: $preview
        );
    }
}
