<?php

namespace App\Http\Controllers;

use App\Models\Employee;
use App\Models\EmployeeProfile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class EmployeeProfileController extends Controller
{
    public function store(Request $request, $employee_id)
    {
        // 1. Validate input
        $validator = Validator::make($request->all(), [
            'full_name'         => 'required|string|max:255',
            'email'             => 'required|email|unique:employee_profiles,email',
            'phone_number'      => 'required|string|max:20',
            'start_date'        => 'required|date',
            'department'        => 'required|string|max:255',
            'position'          => 'required|string|max:255',
            'technical_skills'  => 'nullable|string',
            'professional_bio'  => 'nullable|string',
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }

        // 2. Make sure employee exists
        $employee = Employee::findOrFail($employee_id);

        // 3. Store profile
        $profile = EmployeeProfile::create([
            'employee_id'       => $employee->id, // new line
            'full_name'        => $request->full_name,
            'email'            => $request->email,
            'phone_number'     => $request->phone_number,
            'start_date'       => $request->start_date,
            'department'       => $request->department,
            'position'         => $request->position,
            'technical_skills' => $request->technical_skills,
            'professional_bio' => $request->professional_bio,
        ]);

        return response()->json([
            'message' => 'Profile completed successfully',
            'profile' => $profile,
        ], 201);
    }
}
