<?php

namespace Vanguard\Http\Controllers\Web\Users;

use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Vanguard\Http\Controllers\Controller;
use Vanguard\User;
use DB;
class SubmissionController extends Controller
{
    public function index(Request $request): View
    {
        // $users = $this->users->paginate($perPage = 20, $request->search, $request->status);
        $users = \Illuminate\Support\Facades\DB::table('personnels')->orderBy('id', 'DESC');

        $ranks = [
            '' => __('All Ranks'),
            
            // Commissioned Officers
            __('Commissioned Officers') => [
                'General' => __('General'),
                'Lieutenant General' => __('Lieutenant General'),
                'Major General' => __('Major General'),
                'Brigadier General' => __('Brigadier General'),
                'Colonel' => __('Colonel'),
                'Lieutenant Colonel' => __('Lieutenant Colonel'),
                'Major' => __('Major'),
                'Captain' => __('Captain'),
                'Lieutenant' => __('Lieutenant'),
                'Second Lieutenant' => __('Second Lieutenant'),
            ],

            // Non-Commissioned Officers / Other Ranks
            __('Other Ranks') => [
                'Master Warrant Officer' => __('Master Warrant Officer'),
                'Warrant Officer' => __('Warrant Officer'),
                'Staff Sergeant' => __('Staff Sergeant'),
                'Sergeant' => __('Sergeant'),
                'Corporal' => __('Corporal'),
                'Lance Corporal' => __('Lance Corporal'),
                'Private' => __('Private'),
                'Recruit' => __('Recruit'),
            ]
        ];
        if ($request->filled('rank')) {
            $users->where('rank', $request->rank);
        }

        $users = $users->paginate(20);

        return view('submission.list', compact('users', 'ranks'));
    }

    public function createPersonnel(Request $request)
    {
        // 1. Validate the incoming data
        $validatedData = $request->validate([
            'name'            => 'required|string|max:255',
            'email'           => 'required|email|unique:personnels,email',
            'service_code'    => 'required|string|unique:personnels,service_code',
            'rank'            => 'required|string',
            'position'        => 'nullable|string',
            'age'             => 'nullable|integer',
            'years_of_service'=> 'nullable|integer',
            'passport_number' => 'nullable|string',
            'passport'        => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', // Max 2MB
            'biography'       => 'nullable|string',
        ]);

        // 2. Handle the Passport File Upload
        $passportName = null;
        if ($request->hasFile('passport')) {
            $file = $request->file('passport');
            // Create a unique filename: e.g., servicecode_timestamp.jpg
            $passportName = Str::slug($request->service_code) . '_' . time() . '.' . $file->getClientOriginalExtension();
            
            // Move the file to public/upload/users
            $file->move(public_path('upload/users'), $passportName);
        }

        // 3. Create the record in the database
            DB::table('personnels')->insert([
            'name'             => $request->name,
            'email'            => $request->email,
            'service_code'     => $request->service_code,
            'age'              => $request->age,
            'rank'             => $request->rank,
            'position'         => $request->position,
            'passport_number'  => $request->passport_number,
            'years_of_service' => $request->years_of_service,
            'biography'        => $request->biography,
            'passport'         => $passportName, // Stores just the filename
        ]);

        // 4. Redirect back with a success message
        return redirect()->back()->with('success', __('Personnel registered successfully.'));
    }

    public function show(User $user): View
    {
        return view('user.view', compact('user'));
    }

    public function contact()
    {
        $contacts = \Illuminate\Support\Facades\DB::table('contacts')->orderBy('id', 'DESC')->paginate(20);

        return view('submission.contact', compact('contacts'));
    }


    public function flight()
    {
        $flights = \Illuminate\Support\Facades\DB::table('flight_bookings')->orderBy('id', 'DESC')->paginate(20);

        return view('submission.flight', compact('flights'));
    }

    public function destroy($id)
    {
        // Correctly uses the Query Builder's delete() method
        \Illuminate\Support\Facades\DB::table('personnels')
            ->where('id', $id) // Use where() with column and value
            ->delete(); // Executes the DELETE SQL statement

        return redirect()->route('users.submissions')
            ->withSuccess(__('Submission deleted successfully.'));
    }
}
