<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Branch;
use App\Models\ServiceRequest;
use App\Models\BusinessType;
use App\Models\BusinessModel;
use App\Models\State;
use Illuminate\Http\Request;

class ServiceRequestController extends Controller
{
    /**
     * Display a listing of the service requests.
     */
    public function index()
    {
        $query = ServiceRequest::with(['user', 'businessType', 'businessModel', 'branch']);

        if (auth()->user()->role !== 'admin') {
            $query->where('branch_id', auth()->user()->branch_id);
        }

        $serviceRequests = $query->latest()->paginate(20);

        return view('admin.service-requests.index', compact('serviceRequests'));
    }


    /**
     * Display the specified service request.
     */
    public function show(ServiceRequest $serviceRequest)
    {
        $serviceRequest->load(['user', 'businessType', 'businessModel', 'branch']);

        // Decode JSON data
        $contactInfo = $serviceRequest->contact_info;
        $servicesData = $serviceRequest->services_data;

        return view('admin.service-requests.show', compact('serviceRequest', 'contactInfo', 'servicesData'));
    }

    /**
     * Show the form for editing the specified service request.
     */
    public function edit(ServiceRequest $serviceRequest)
    {
        $serviceRequest->load(['user', 'businessType', 'businessModel', 'branch']);

        $businessTypes = BusinessType::where('is_active', true)->get();
        $businessModels = BusinessModel::where('is_active', true)->get();
        $branches = Branch::latest()->get();

        $contactInfo = $serviceRequest->contact_info;
        $servicesData = $serviceRequest->services_data;

        $statuses = [
            'pending' => 'Pending',
            'in_progress' => 'In Progress',
            'approved' => 'Approved',
            'cancelled' => 'Cancelled'
        ];

        return view('admin.service-requests.edit', compact(
            'serviceRequest',
            'businessTypes',
            'businessModels',
            'branches',
            'contactInfo',
            'servicesData',
            'statuses'
        ));
    }

    /**
     * Update the specified service request in storage.
     */
    public function update(Request $request, ServiceRequest $serviceRequest)
    {
        $request->validate([
            'status' => 'required|in:pending,in_progress,approved,cancelled',
            'business_type_id' => 'required|exists:business_types,id',
            'business_model_id' => 'required|exists:business_models,id',
            'branch_id' => 'required|exists:branches,id',
            'total_price' => 'required|numeric|min:0',
            'admin_notes' => 'nullable|string|max:1000'
        ]);

        $serviceRequest->update([
            'status' => $request->status,
            'business_type_id' => $request->business_type_id,
            'business_model_id' => $request->business_model_id,
            'branch_id' => $request->branch_id,
            'total_price' => $request->total_price,
            'admin_notes' => $request->admin_notes
        ]);




        return redirect()->route('admin.service-requests.index')
            ->with('success', 'Service request updated successfully.');
    }

    /**
     * Remove the specified service request from storage.
     */
    public function destroy(ServiceRequest $serviceRequest)
    {
        $serviceRequest->delete();

        return redirect()->route('admin.service-requests.index')
            ->with('success', 'Service request deleted successfully.');
    }

    /**
     * Update service request status
     */
    public function updateStatus(Request $request, ServiceRequest $serviceRequest)
    {
        $request->validate([
            'status' => 'required|in:pending,in_progress,approved,cancelled'
        ]);

        $serviceRequest->update([
            'status' => $request->status
        ]);

        $statusLabels = [
            'pending' => 'pending',
            'in_progress' => 'in progress',
            'approved' => 'Approved',
            'cancelled' => 'cancelled'
        ];

        return redirect()->route('admin.service-requests.index')
            ->with('success', "Service request status updated to {$statusLabels[$request->status]} successfully.");
    }
}