<?php

namespace App\Http\Controllers;

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

class ApplicationController extends Controller
{
    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'career_id' => 'required|exists:careers,id',
            'name' => 'required|string|max:255',
            'birthdate' => 'required|date',
            'resume_file' => 'required|file|mimes:pdf,doc,docx|max:2048',
            'email' => 'required|email|max:255',
            'phone_number' => 'required|string',
            'cover_letter' => 'nullable|string',
        ]);

        $resumePath = $request->file('resume_file')->store('resumes');

        Application::create([
            'career_id' => $validatedData['career_id'],
            'name' => $validatedData['name'],
            'birthdate' => $validatedData['birthdate'],
            'resume_file' => $resumePath,
            'email' => $validatedData['email'],
            'phone_number' => $validatedData['phone_number'],
            'cover_letter' => $validatedData['cover_letter'] ?? null,
        ]);

        return response()->json([
            'message' => 'Your application has been submitted successfully!',
        ]);
    }
}
