<?php

namespace App\Http\Controllers\api\v1\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\VendorSubCategory\AddVendorSubCategoryRequest;
use App\Models\VendorSubCategory;
use App\Service\Admin\VendorSubCategoryService;
use App\Traits\JsonResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class VendorSubCategoryController extends Controller
{
    use JsonResponseTrait;

    protected $vendorSubCategoryService;

    public function __construct(VendorSubCategoryService $vendorSubCategoryService)
    {
        $this->vendorSubCategoryService = $vendorSubCategoryService;
    }

    /**
     * Get a list of all vendor categories.
     */
    // public function index()
    // {
    //     $categories = $this->vendorSubCategoryService->getAll();
    //     return $this->successDataResponse($categories);
    // }

    /**
     * Store a new vendor category.
     */
    public function createVendorSubCategory(AddVendorSubCategoryRequest $request)
    {
        $input = $request->validated();
        if (Auth::check()) {
            $input['created_by'] = Auth::user()->admin_id;
        } else {
            return $this->errorResponse(403, 'Unauthorized action');
        }

        $category = $this->vendorSubCategoryService->createVendorSubCategory($input);
        return $this->successDataResponse(data: $category, message: 'Vendor sub category created successfully');
    }

    /**
     * Show a specific vendor category.
     */
    public function getVendorSubCategories($track_id = null)
    {
        if ($track_id) {
            $vendorSubCategory = VendorSubCategory::where('track_id', $track_id)->first();
            if (!$vendorSubCategory) {
                return $this->errorResponse(403, 'Vendor sub category not found');
            }
            return response()->json($vendorSubCategory);
        }

        // No track_id provided – return all
        $allCategories = VendorSubCategory::all();
        return response()->json($allCategories);
    }


    /**
     * Update an existing vendor category.
     */
    public function updateVendorSubCategory(Request $request)
    {
        // Validate that track_id is present in the request
        $request->validate([
            'track_id' => 'required|string|exists:vendor_sub_categories,track_id',
        ]);

        // Get the category using track_id from the request body
        $category = VendorSubCategory::where('track_id', $request->track_id)->first();

        if (!$category) {
            return $this->errorResponse(404, 'Vendor sub category not found');
        }

        // Perform the update
        $updatedCategory = $this->vendorSubCategoryService->updateVendorSubCategory($category, $request->all());

        return response()->json([
            'status' => true,
            'message' => 'Vendor category updated successfully',
            'data' => $updatedCategory,
        ]);
    }



    /**
     * Delete a vendor category.
     */
    public function deleteVendorCategory(Request $request)
    {
        // Validate input
        $request->validate([
            'track_id' => 'required|string|exists:vendor_sub_categories,track_id',
        ]);

        $deleted = $this->vendorSubCategoryService->deleteVendorSubCategory($request->track_id);

        if (!$deleted) {
            return $this->errorResponse(404, 'Vendor sub category not found or already deleted');
        }

        return $this->successDataResponse(200, message: 'Vendor sub category deleted successfully');

    }
}
