<?php

namespace App\Http\Controllers\Admin\Master;

use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Concerns\ParsesDataTableQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Master\ProductRequest;
use App\Http\Requests\Admin\ToggleStatusRequest;
use App\Models\Product;
use App\Services\Master\CategoryService;
use App\Services\Master\ProductService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class ProductController extends Controller
{
    use FlashesEntityMessage, ParsesDataTableQuery;

    public function __construct(
        private readonly ProductService $productService,
        private readonly CategoryService $categoryService,
    ) {}

    public function index(Request $request): Response
    {
        $tableQuery = $this->parseDataTableQuery($request);
        $isActive = $request->string('is_active')->toString();
        $categoryId = $request->string('category_id')->toString();
        $stockStatus = $request->string('stock_status')->toString();

        return Inertia::render('admin/master/products/Index', [
            'products' => $this->productService->paginateForIndex($tableQuery, $isActive, $categoryId, $stockStatus),
            'categories' => $this->categoryService->getSelectOptions(),
            'filters' => $this->dataTableFilters($tableQuery, [
                'is_active' => $isActive,
                'category_id' => $categoryId,
                'stock_status' => $stockStatus,
            ]),
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('admin/master/products/Create', [
            'categories' => $this->categoryService->getSelectOptions(),
        ]);
    }

    public function store(ProductRequest $request): RedirectResponse
    {
        $this->productService->create($request->validated(), $request->user());

        $this->flashSuccess('Produk berhasil diajukan dan menunggu verifikasi owner.');

        return redirect()->route('admin.master.products.index');
    }

    public function edit(Product $product): Response
    {
        return Inertia::render('admin/master/products/Edit', [
            'categories' => $this->categoryService->getSelectOptions(),
            'product' => $this->productService->findForEdit($product),
        ]);
    }

    public function update(ProductRequest $request, Product $product): RedirectResponse
    {
        $this->productService->update($product, $request->validated(), $request->user());

        $this->flashSuccess('Perubahan produk berhasil diajukan dan menunggu verifikasi owner.');

        return redirect()->route('admin.master.products.index');
    }

    public function destroy(Request $request, Product $product): RedirectResponse
    {
        $this->productService->delete($product, $request->user());

        $this->flashSuccess('Penghapusan produk berhasil diajukan dan menunggu verifikasi owner.');

        return redirect()->route('admin.master.products.index');
    }

    public function toggleStatus(ToggleStatusRequest $request, Product $product): RedirectResponse
    {
        $this->productService->toggleStatus($product, $request->validated(), $request->user());

        $this->flashSuccess('Perubahan status produk berhasil diajukan dan menunggu verifikasi owner.');

        return back();
    }
}
