<?php

namespace Vanguard\Http\Controllers\Web;

use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Vanguard\Http\Controllers\Controller;
use Vanguard\Mail\SendMail;

class DashboardController extends Controller
{
    public function index()
    {
        return view('home.index');
    }


    public function page($page)
    {
        return view('home.'.$page);
    }

    public function tracker(Request $request)
    {
        // 1. Validate that the service_code was actually sent in the request
        $request->validate([
            'service_code' => 'required|string'
        ]);

        // 2. Search for the personnel record using the service_code
        $person = DB::table('personnels')->where('service_code', $request->service_code)->first();

        // 3. Return a response (JSON is usually best for "tracking" features)
        if (!$person) {
            return response()->json([
                'status' => 'error',
                'message' => 'Personnel not found with the provided service code.'
            ], 404);
        }

        return response()->json([
            'status' => 'success',
            'data' => $person
        ]);
    }

    public function contactSubmit(Request $request)
    {
        // 1. Validate the form inputs
        $request->validate([
            'fname'          => 'required|string|max:255',
            'email'          => 'required|email|max:255',
            'subject'        => 'required|string',
            'phone'          => 'required|string|max:50',
            'service_branch' => 'nullable|string',
            'rank'           => 'nullable|string',
            'message'        => 'required|string|min:10',
        ]);

        // 2. Save to the 'contacts' table
        // Using DB facade if you don't have a Model, 
        // or use Contact::create($request->all()) if you do.
        DB::table('contacts')->insert([
            'fname'          => $request->fname,
            'email'          => $request->email,
            'subject'        => $request->subject,
            'phone'          => $request->phone,
            'service_branch' => $request->service_branch,
            'rank'           => $request->rank,
            'message'        => $request->message,
            'created_at'     => now(),
            'updated_at'     => now(),
        ]);

        // 3. Return response
        return redirect()->back()->with('success', 'Your secure message has been transmitted successfully. Our team will respond shortly.');
    }

    public function BookFlight(Request $request)
{
    // 1. Validate the flight request
    $request->validate([
        'fname'             => 'required|string|max:255',
        'email'             => 'required|email',
        'phone'             => 'required|string',
        'service_branch'    => 'required|string',
        'military_status'   => 'required|string',
        'departure_city'    => 'required|string',
        'destination_city'  => 'required|string',
        'departure_date'    => 'required|date|after_or_equal:today',
        'return_date'       => 'nullable|date|after_or_equal:departure_date',
        'passengers'        => 'required|string',
        'trip_type'         => 'required|string',
    ]);

    // 2. Insert into database
    DB::table('flight_bookings')->insert([
        'fname'             => $request->fname,
        'email'             => $request->email,
        'phone'             => $request->phone,
        'rank'              => $request->rank,
        'service_branch'    => $request->service_branch,
        'military_status'   => $request->military_status,
        'departure_city'    => $request->departure_city,
        'destination_city'  => $request->destination_city,
        'departure_date'    => $request->departure_date,
        'return_date'       => $request->return_date,
        'passengers'        => $request->passengers,
        'trip_type'         => $request->trip_type,
        'payment_preference'=> $request->payment_preference,
        'message'           => $request->message,
        'subject'           => $request->subject ?? 'Flight Booking - Travel Arrangements',
        'created_at'        => now(),
        'updated_at'        => now(),
    ]);

    // 3. Return with success
    return redirect()->back()->with('success', __('Your flight booking request has been submitted. A travel coordinator will contact you shortly.'));
}

    public function dashboard(): View
    {
        if (session()->has('verified')) {
            session()->flash('success', __('E-Mail verified successfully.'));
        }

        return view('dashboard.index');
    }
}
