<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\CustomQuote;
use App\Models\AutoQuote;

class QuoteController extends Controller
{
    public function createQuote(Request $request)
{
    try {

        $type = $request->type;

        if ($type === 'custom') {

            $quote = CustomQuote::create([
                'name' => $request->name,
                'phone_number' => $request->phone_number,
                'email' => $request->email,
                'preferred_language' => $request->preferred_language,
                'full_address' => $request->full_address,

                'coverage_auto' => $request->coverage_auto ?? 0,
                'coverage_home' => $request->coverage_home ?? 0,
                'coverage_commercial' => $request->coverage_commercial ?? 0,
                'coverage_life' => $request->coverage_life ?? 0,
                'coverage_travel' => $request->coverage_travel ?? 0,
                'coverage_super_bonds' => $request->coverage_super_bonds ?? 0,
                'coverage_bundle_save' => $request->coverage_bundle_save ?? 0,
                'coverage_other_details' => $request->coverage_other_details,

                'currently_insured' => $request->currently_insured,
                'current_insurance_provider' => $request->current_insurance_provider,

                'preferred_contact_method' => $request->preferred_contact_method,
                'best_time_to_reach' => $request->best_time_to_reach
            ]);

            return response()->json([
                'status' => true,
                'message' => 'Custom Quote Created Successfully',
                'data' => $quote
            ], 201);
        }

	if ($type === 'auto') {
            $quote = AutoQuote::create([
                'type'                 => $request->type,
                'first_name'           => $request->first_name,
                'last_name'            => $request->last_name,
                'zip_code'             => $request->zip_code,
                'email_address'        => $request->email_address,
                'phone_number'         => $request->phone_number,
                'vehicles' => json_encode($request->vehicles), 
    'drivers'  => json_encode($request->drivers),
                'currently_issued'     => $request->currently_issued,
                'sr_22_needed'         => $request->sr_22_needed,
                'bundle_home'          => $request->bundle_home ?? false,
                'bundle_auto'          => $request->bundle_auto ?? false,
                'bundle_health'        => $request->bundle_health ?? false,
                'just_auto'            => $request->just_auto ?? false,

                'preferred_language'   => $request->preferred_language,
                'contact_method'       => $request->contact_method,
                'best_time_to_connect' => $request->best_time_to_connect,
            ]);

            return response()->json([
                'status'  => true,
                'message' => 'Auto Quote Created Successfully',
                'data'    => $quote
            ], 201);
        }

        return response()->json([
            'status' => false,
            'message' => 'Invalid Quote Type'
        ], 400);

    } catch (\Exception $e) {

        return response()->json([
            'status' => false,
            'error' => $e->getMessage()
        ], 500);
    }
}
}
