<?php

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

use App\Http\Requests\Vendor\CreateVendorServiceRequest;
use App\Http\Requests\Vendor\UpdateVendorServiceRequest;
use App\Models\VendorService;
use App\Service\Vendor\VendorServiceService;
use App\Traits\JsonResponseTrait;
use Illuminate\Http\Request;

class VendorServiceController
{
    use JsonResponseTrait;

    protected $vendorServiceService;

    public function __construct(VendorServiceService $vendorServiceService)
    {
        $this->vendorServiceService = $vendorServiceService;
    }

    /**
     * Get all vendor services
     */
    public function index(Request $request)
    {
        $vendor = $request->auth_user;
        
        $services = $this->vendorServiceService->getAllServices(
            $vendor->user_id,
            $request->get('status'),
            $request->get('category'),
            $request->get('featured')
        );

        return $this->successDataResponse(
            data: $services,
            message: 'Services retrieved successfully'
        );
    }

    /**
     * Get a specific vendor service
     */
    public function show($serviceId)
    {
        $service = $this->vendorServiceService->getServiceById($serviceId);

        return $this->successDataResponse(
            data: $service,
            message: 'Service retrieved successfully'
        );
    }

    /**
     * Create a new vendor service
     */
    public function store(CreateVendorServiceRequest $request)
    {
        $vendor = $request->auth_user;
        
        $service = $this->vendorServiceService->createService($vendor, $request);

        return $this->successDataResponse(
            data: $service,
            message: 'Service submitted for approval successfully'
        );
    }

    /**
     * Update an existing vendor service
     */
    public function update($serviceId, UpdateVendorServiceRequest $request)
    {
        $service = VendorService::findOrFail($serviceId);
        
        $updatedService = $this->vendorServiceService->updateService($service, $request);

        return $this->successDataResponse(
            data: $updatedService,
            message: 'Service updated successfully'
        );
    }

    /**
     * Delete a vendor service
     */
    public function destroy($serviceId)
    {
        $service = VendorService::findOrFail($serviceId);
        
        $this->vendorServiceService->deleteService($service);

        return $this->successResponse(message: 'Service deleted successfully');
    }

    /**
     * Approve a vendor service
     */
    public function approve($serviceId)
    {
        $service = VendorService::findOrFail($serviceId);
        
        $this->vendorServiceService->approveService($service);

        return $this->successResponse(message: 'Service approved successfully');
    }

    /**
     * Reject a vendor service
     */
    public function reject($serviceId, Request $request)
    {
        $service = VendorService::findOrFail($serviceId);
        $reason = $request->input('reason');
        
        $this->vendorServiceService->rejectService($service, $reason);

        return $this->successResponse(message: 'Service rejected successfully');
    }

    /**
     * Toggle featured status of a service
     */
    public function toggleFeatured($serviceId)
    {
        $service = VendorService::findOrFail($serviceId);
        
        $this->vendorServiceService->toggleFeatured($service);

        return $this->successResponse(message: 'Service featured status updated successfully');
    }
}
