<?php

namespace App\Http\Controllers;

use App\Models\JobPosting;
use App\Models\JobApplication;
use App\Http\Requests\StoreJobApplicationRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Mail\JobApplicationSubmitted;
use Illuminate\Support\Facades\Mail;

class CareerController extends Controller
{
    public function index()
    {
        $jobPostings = JobPosting::where('is_active', true)
            ->orderBy('created_at', 'desc')
            ->get();

        return view('career', compact('jobPostings'));
    }

    public function show(JobPosting $jobPosting)
    {
        // Check if the job posting is active
        if (!$jobPosting->is_active) {
            abort(404);
        }

        return view('career-detail', compact('jobPosting'));
    }

    public function apply(StoreJobApplicationRequest $request, JobPosting $jobPosting)
{
    // Log request data
    \Log::info('Job application request', [
        'request_data' => $request->all(),
        'has_file' => $request->hasFile('resume'),
        'file_info' => $request->hasFile('resume') ? [
            'original_name' => $request->file('resume')->getClientOriginalName(),
            'size' => $request->file('resume')->getSize(),
            'mime_type' => $request->file('resume')->getMimeType(),
        ] : null,
    ]);

    // Handle file upload
    $resumePath = null;
    if ($request->hasFile('resume')) {
        try {
            $resumePath = $request->file('resume')->store('resumes');
            \Log::info('File uploaded successfully', ['path' => $resumePath]);
        } catch (\Exception $e) {
            \Log::error('File upload failed', ['error' => $e->getMessage()]);
            return back()->with('error', 'Failed to upload resume. Please try again.');
        }
    }

    try {
        // Create job application
        $jobApplication = JobApplication::create([
            'job_posting_id' => $jobPosting->id,
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'email' => $request->email,
            'phone' => $request->phone,
            'resume_path' => $resumePath,
            'cover_letter' => $request->cover_letter,
        ]);

        \Log::info('Job application created', ['id' => $jobApplication->id]);

        // Send email notification to specific email address
        $recipientEmail = 'mrizalabidinn@gmail.com';
        \Log::info('Attempting to send email', ['to' => $recipientEmail]);
        
        Mail::to($recipientEmail)->send(new JobApplicationSubmitted($jobApplication));
        \Log::info('Email sent successfully');

        return redirect()->route('career.show', $jobPosting->slug)
            ->with('success', 'Your application has been submitted successfully!');
    } catch (\Exception $e) {
        \Log::error('Job application failed', ['error' => $e->getMessage()]);
        return back()->with('error', 'Failed to submit application. Please try again.');
    }
}
}