<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\RequestModel;
use Carbon\Carbon;

class RequestController extends Controller
{
    /**
     * Handle new user signup request (registration request)
     */
    public function usersignup(Request $request)
    {
        // ✅ Validate user input
        $request->validate([
            'customer_name' => 'required|string|max:255',
            'company_name' => 'required|string|max:255',
            'designation' => 'nullable|string|max:255',
            'email' => 'required|email|unique:requests,email',
            'mobile_no' => 'required|string|max:15',
            'priority' => 'nullable|string|max:50',
            'address' => 'nullable|string',
            'pincode' => 'nullable|string|max:10',
        ]);

        // ✅ Generate formatted request ID (e.g., REQ-2025-000001)
        $lastRequest = RequestModel::latest('id')->first();
        $nextId = $lastRequest ? $lastRequest->id + 1 : 1;
        $year = date('Y');
        $formattedRequestId = sprintf('REQ-%s-%06d', $year, $nextId);

        // ✅ Create new request entry
        $newRequest = RequestModel::create([
            'request_id' => $formattedRequestId,
            'customer_name' => $request->customer_name,
            'company_name' => $request->company_name,
            'designation' => $request->designation,
            'email' => $request->email,
            'mobile_no' => $request->mobile_no,
            'priority' => $request->priority,
            'address' => $request->address,
            'pincode' => $request->pincode,
            'date' => Carbon::now()->toDateString(), // Auto current date
            'status' => 'pending', // Default status
        ]);

        // ✅ Response
        return response()->json([
            'status' => true,
            'message' => 'Signup request submitted successfully. Please wait for admin approval.',
            'data' => $newRequest
        ]);
    }

        
}
