<?php

namespace App\Http\Controllers\HR;

use App\Http\Controllers\Controller;
use App\Models\Boards\File;
use App\Services\HR\EmployeeService;
use Illuminate\Contracts\View\View;

class EmployeeController extends Controller
{
    public function __construct(
        private EmployeeService $employeeService
    ) {}

    /**
     * 사원 목록 페이지
     */
    public function index(): View
    {
        $stats = $this->employeeService->getStats();
        $departments = $this->employeeService->getDepartments();

        return view('hr.employees.index', [
            'stats' => $stats,
            'departments' => $departments,
        ]);
    }

    /**
     * 사원 등록 폼
     */
    public function create(): View
    {
        $departments = $this->employeeService->getDepartments();
        $ranks = $this->employeeService->getPositions('rank');
        $titles = $this->employeeService->getPositions('title');

        return view('hr.employees.create', [
            'departments' => $departments,
            'ranks' => $ranks,
            'titles' => $titles,
            'banks' => config('banks', []),
        ]);
    }

    /**
     * 사원 상세 페이지
     */
    public function show(int $id): View
    {
        $employee = $this->employeeService->getEmployeeById($id);

        if (! $employee) {
            abort(404, '사원 정보를 찾을 수 없습니다.');
        }

        $files = File::where('document_type', 'employee_profile')
            ->where('document_id', $employee->id)
            ->where('tenant_id', session('selected_tenant_id'))
            ->orderBy('created_at', 'desc')
            ->get();

        return view('hr.employees.show', [
            'employee' => $employee,
            'files' => $files,
        ]);
    }

    /**
     * 사원 수정 폼
     */
    public function edit(int $id): View
    {
        $employee = $this->employeeService->getEmployeeById($id);

        if (! $employee) {
            abort(404, '사원 정보를 찾을 수 없습니다.');
        }

        $departments = $this->employeeService->getDepartments();
        $ranks = $this->employeeService->getPositions('rank');
        $titles = $this->employeeService->getPositions('title');

        $files = File::where('document_type', 'employee_profile')
            ->where('document_id', $employee->id)
            ->where('tenant_id', session('selected_tenant_id'))
            ->orderBy('created_at', 'desc')
            ->get();

        return view('hr.employees.edit', [
            'employee' => $employee,
            'departments' => $departments,
            'ranks' => $ranks,
            'titles' => $titles,
            'banks' => config('banks', []),
            'files' => $files,
        ]);
    }
}
