<?php

namespace App\Http\Controllers;

use App\Models\Courier;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CourierController extends Controller
{
    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'required|string|max:20',
            'collection_address' => 'required|string|max:255',
            'delivery_address' => 'required|string|max:255',
            'collection_date' => 'nullable|date',
            'service_type' => 'required|string|max:100',
            'package_details' => 'required|string',
            'message' => 'nullable|string',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'message' => 'Validation error',
                'errors' => $validator->errors()
            ], 422);
        }

        try {
            $courier = Courier::create($request->all());

            return response()->json([
                'success' => true,
                'message' => 'Courier quote requested successfully',
                'data' => $courier
            ], 201);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to request quote: ' . $e->getMessage()
            ], 500);
        }
    }
}
