<?php

namespace App\Http\Controllers;

use App\Models\Internship;
use Illuminate\Http\Request;
use App\Models\InternshipApplication; // Changed from App\Models\Application

class InternshipsPageController extends Controller
{
    public function index()
    {
        $internships = Internship::query()->get();

        return view('web.pages.internships.index', compact('internships'));
    }

    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'internship_id' => 'required|exists:internships,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');

        InternshipApplication::create([ // Changed to InternshipApplication::create
            'internship_id' => $validatedData['internship_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!',
        ]);
    }

    public function show(Internship $internship)
    {
        return view('web.pages.internships.show', compact('internship'));
    }
}
