<?php

namespace App\Http\Controllers;

use App\Models\Quote;
use App\Mail\QuoteConfirmation;
use App\Mail\QuoteNotification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class QuoteController extends Controller
{
    /**
     * Display a listing of all quotes.
     */
    public function index()
    {
        $quotes = Quote::orderBy('created_at', 'desc')->get();
        return response()->json([
            'success' => true,
            'data' => $quotes
        ]);
    }

    /**
     * Store a newly created quote in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'required|string|max:20',
            'move_from' => 'required|string|max:255',
            'move_to' => 'required|string|max:255',
            'move_date' => 'nullable|date',
            'property_size' => 'nullable|string|max:50',
            'services' => 'nullable|array',
            'message' => 'nullable|string',
        ]);

        $quote = Quote::create($validated);

        // Send email to Client
        // Send email to Client
        try {
            Mail::to($quote->email)->send(new QuoteConfirmation($quote));
            \Illuminate\Support\Facades\Log::info("Quote confirmation email sent to: " . $quote->email);
        } catch (\Exception $e) {
            \Illuminate\Support\Facades\Log::error("Failed to send quote confirmation email to " . $quote->email . ": " . $e->getMessage());
        }

        // Send email to Admin
        $adminEmail = config('mail.from.address'); 
        \Illuminate\Support\Facades\Log::info("Admin email configured as: " . ($adminEmail ?? 'NULL'));

        if ($adminEmail) {
            try {
                Mail::to($adminEmail)->send(new QuoteNotification($quote));
                \Illuminate\Support\Facades\Log::info("Quote notification email sent to admin: " . $adminEmail);
            } catch (\Exception $e) {
                \Illuminate\Support\Facades\Log::error("Failed to send quote notification email to admin " . $adminEmail . ": " . $e->getMessage());
            }
        } else {
             \Illuminate\Support\Facades\Log::warning("Admin email not configured, skipping notification.");
        }

        return response()->json([
            'success' => true,
            'message' => 'Quote submitted successfully',
            'data' => $quote
        ], 201);
    }

    /**
     * Display the specified quote.
     */
    public function show($id)
    {
        $quote = \App\Models\Quote::findOrFail($id);
        return response()->json([
            'success' => true,
            'data' => $quote
        ]);
    }

    /**
     * Remove the specified quote from storage.
     */
    public function destroy($id)
    {
        $quote = \App\Models\Quote::findOrFail($id);
        $quote->delete();

        return response()->json([
            'success' => true,
            'message' => 'Quote deleted successfully'
        ]);
    }
}
