<?php

namespace App\Http\Controllers;

use App\Models\JobApplication;
use Illuminate\Http\Request;

class JobApplicationController extends Controller
{
    //===========================================
    // Dashboard - All Applications
    //===========================================

    public function index()
    {
        $applications = JobApplication::latest()->get();

        return view('dashboard.Job_Application.index', compact('applications'));
    }

    //===========================================
    // Store Career Application
    //===========================================

    public function store(Request $request)
    {
        $request->validate([

            'full_name'      => 'required|string|max:255',

            'email'          => 'required|email|max:255',

            'phone'          => 'required|max:30',

            'position'       => 'required|max:255',

            'resume'         => 'required|mimes:pdf,doc,docx|max:5120',

            'cover_letter'   => 'nullable|string',

        ]);

        $resumeName = null;

        if ($request->hasFile('resume')) {

            $resumeName = time() . '_' . $request->resume->getClientOriginalName();

            $request->resume->move(
                public_path('uploads/resumes'),
                $resumeName
            );
        }

        JobApplication::create([

            'full_name'      => $request->full_name,

            'email'          => $request->email,

            'phone'          => $request->phone,

            'position'       => $request->position,

            'resume'         => $resumeName,

            'cover_letter'   => $request->cover_letter,

            'status'         => 'Pending',

        ]);

        return back()->with(
            'success',
            'Your application has been submitted successfully.'
        );
    }

    //===========================================
    // View Single Application
    //===========================================

    public function show($id)
    {
        $application = JobApplication::findOrFail($id);

        return view(
            'dashboard.applications.show',
            compact('application')
        );
    }

    //===========================================
    // Download Resume
    //===========================================

    public function downloadResume($id)
    {
        $application = JobApplication::findOrFail($id);

        $path = public_path('uploads/resumes/' . $application->resume);

        if (!file_exists($path)) {

            abort(404);
        }

        return response()->download($path);
    }

    //===========================================
    // Delete Application
    //===========================================

    public function destroy($id)
    {
        $application = JobApplication::findOrFail($id);

        if (
            $application->resume &&
            file_exists(public_path('uploads/resumes/' . $application->resume))
        ) {

            unlink(public_path('uploads/resumes/' . $application->resume));
        }

        $application->delete();

        return redirect()
            ->route('applications.index')
            ->with('success', 'Application Deleted Successfully.');
    }

    public function updateStatus(Request $request, $id)
{
    $request->validate([
        'status' => 'required|in:Pending,Reviewed,Shortlisted,Rejected'
    ]);

    $application = JobApplication::findOrFail($id);

    $application->status = $request->status;

    $application->save();

    return back()->with('success', 'Application Status Updated Successfully.');
}
}
