<?php
declare(strict_types=1);

namespace App\Modules\HR\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Core\App;
use App\Core\Logger;
use App\Modules\HR\Models\HrEmployeeDocument;
use App\Modules\HR\Models\HrEmployeeProfile;

class EmployeeDocumentController extends Controller
{
    public function index(Request $request): Response
    {
        $filters = [
            'q'             => trim((string) $request->get('q', '')),
            'document_type' => trim((string) $request->get('document_type', '')),
            'expiring_soon' => trim((string) $request->get('expiring_soon', '')),
        ];

        $page = max(1, (int) $request->get('page', 1));
        $result = HrEmployeeDocument::search($filters, 25, $page);

        return $this->view('HR.Views.documents.index', [
            'documents'     => $result['data'],
            'pagination'    => $result['pagination'],
            'filters'       => $filters,
            'documentTypes' => HrEmployeeDocument::getDocumentTypes(),
        ]);
    }

    public function byEmployee(Request $request, string $employeeId): Response
    {
        $profile = HrEmployeeProfile::find((int) $employeeId);
        if (!$profile) {
            return $this->redirect('/hr/documents')->withError('الموظف غير موجود');
        }

        $db = App::getInstance()->db();
        $documents = $db->select(
            "SELECT * FROM hr_employee_documents WHERE employee_profile_id = ? AND is_archived = 0 ORDER BY created_at DESC",
            [(int) $employeeId]
        );

        return $this->view('HR.Views.documents.by_employee', [
            'profile'       => $profile,
            'documents'     => $documents,
            'documentTypes' => HrEmployeeDocument::getDocumentTypes(),
        ]);
    }

    public function upload(Request $request): Response
    {
        $profileId = (int) $request->post('employee_profile_id', 0);
        $documentType = trim((string) $request->post('document_type', ''));
        $documentNumber = trim((string) $request->post('document_number', '')) ?: null;
        $issueDate = trim((string) $request->post('issue_date', '')) ?: null;
        $expiryDate = trim((string) $request->post('expiry_date', '')) ?: null;
        $notes = trim((string) $request->post('notes', '')) ?: null;

        $errors = [];
        if ($profileId <= 0) {
            $errors[] = 'الموظف غير محدد';
        }
        if ($documentType === '') {
            $errors[] = 'نوع المستند مطلوب';
        }

        // Handle file upload
        $filePath = null;
        if (isset($_FILES['document_file']) && $_FILES['document_file']['error'] === UPLOAD_ERR_OK) {
            $allowedTypes = ['application/pdf', 'image/jpeg', 'image/png', 'image/jpg'];
            $fileType = $_FILES['document_file']['type'];
            $fileSize = $_FILES['document_file']['size'];

            if (!in_array($fileType, $allowedTypes, true)) {
                $errors[] = 'نوع الملف غير مسموح. المسموح: PDF, JPEG, PNG';
            }
            if ($fileSize > 5 * 1024 * 1024) {
                $errors[] = 'حجم الملف يجب أن يكون أقل من 5 ميجابايت';
            }

            if (empty($errors)) {
                $uploadDir = 'uploads/hr/documents/' . $profileId;
                $absUploadDir = rtrim($_SERVER['DOCUMENT_ROOT'] ?? '', '/') . '/' . $uploadDir;

                if (!is_dir($absUploadDir)) {
                    mkdir($absUploadDir, 0755, true);
                }

                $ext = pathinfo($_FILES['document_file']['name'], PATHINFO_EXTENSION);
                $fileName = $documentType . '_' . date('Ymd_His') . '.' . $ext;
                $targetPath = $absUploadDir . '/' . $fileName;

                if (move_uploaded_file($_FILES['document_file']['tmp_name'], $targetPath)) {
                    $filePath = $uploadDir . '/' . $fileName;
                } else {
                    $errors[] = 'فشل رفع الملف';
                }
            }
        }

        if (!empty($errors)) {
            $session = App::getInstance()->session();
            $session->flash('_alerts', array_map(fn($e) => ['type' => 'error', 'message' => $e], $errors));
            $session->flash('_old_input', $request->all());
            return $this->redirect('/hr/documents/employee/' . $profileId);
        }

        $db = App::getInstance()->db();
        $employee = $this->currentEmployee();

        $docId = $db->insert('hr_employee_documents', [
            'employee_profile_id' => $profileId,
            'document_type'       => $documentType,
            'document_number'     => $documentNumber,
            'issue_date'          => $issueDate,
            'expiry_date'         => $expiryDate,
            'file_path'           => $filePath,
            'notes'               => $notes,
            'is_verified'         => 0,
            'created_at'          => date('Y-m-d H:i:s'),
            'updated_at'          => date('Y-m-d H:i:s'),
            'created_by'          => $employee ? (int) $employee->id : null,
        ]);

        Logger::info("Document uploaded", ['document_id' => $docId, 'profile_id' => $profileId, 'type' => $documentType]);

        return $this->redirect('/hr/documents/employee/' . $profileId)->withSuccess('تم رفع المستند بنجاح');
    }

    public function verify(Request $request, string $id): Response
    {
        $document = HrEmployeeDocument::find((int) $id);
        if (!$document) {
            return $this->redirect('/hr/documents')->withError('المستند غير موجود');
        }

        $db = App::getInstance()->db();
        $employee = $this->currentEmployee();

        $db->update('hr_employee_documents', [
            'is_verified'  => 1,
            'verified_by'  => $employee ? (int) $employee->id : null,
            'verified_at'  => date('Y-m-d H:i:s'),
            'updated_at'   => date('Y-m-d H:i:s'),
        ], '`id` = ?', [(int) $id]);

        return $this->redirect('/hr/documents/employee/' . $document->employee_profile_id)->withSuccess('تم التحقق من المستند');
    }

    public function archive(Request $request, string $id): Response
    {
        $document = HrEmployeeDocument::find((int) $id);
        if (!$document) {
            return $this->redirect('/hr/documents')->withError('المستند غير موجود');
        }

        $profileId = $document->employee_profile_id;
        $document->archive();

        return $this->redirect('/hr/documents/employee/' . $profileId)->withSuccess('تم حذف المستند');
    }
}
