<?php
require_once __DIR__ . '/../../../Models/Maid.php';

class MaidController {
    public function index() {
        $maidModel = new Maid();
        
        $page = isset($_GET['p']) ? (int)$_GET['p'] : 1;
        $perPage = 10;
        
        $result = $maidModel->paginateWithDetails($page, $perPage);
        $maids = $result['data'];
        
        $total = $result['total'];
        $totalPages = ceil($total / $perPage);

        require __DIR__ . '/../../../../views/admin/maids/index.php';
    }
    public function show() {
        $id = $_GET['id'] ?? 0;
        $db = Database::getInstance();
        
        $stmt = $db->prepare("
            SELECT m.*,
                   n.name_en as nationality_name, n.name_ar as nationality_name_ar,
                   sm.name_en as service_model_name, sm.name_ar as service_model_name_ar,
                   at.name_en as accommodation_type_name, at.name_ar as accommodation_type_name_ar,
                   v.agency_name
            FROM maids m
            LEFT JOIN nationalities n ON m.nationality_id = n.id
            LEFT JOIN service_models sm ON m.service_model_id = sm.id
            LEFT JOIN accommodation_types at ON m.accommodation_type_id = at.id
            LEFT JOIN vendors v ON m.vendor_id = v.id
            WHERE m.id = ?
        ");
        $stmt->execute([$id]);
        $maid = $stmt->fetch();
        
        if (!$maid) {
            header("Location: ?page=maids&error=Maid not found");
            exit;
        }
        
        // Languages
        $langStmt = $db->prepare("
            SELECT l.name_en, l.name_ar 
            FROM maid_languages ml
            JOIN languages l ON ml.language_id = l.id
            WHERE ml.maid_id = ?
        ");
        $langStmt->execute([$id]);
        $languages = $langStmt->fetchAll();
        
        // Skills
        $skillStmt = $db->prepare("
            SELECT s.name_en, s.name_ar 
            FROM maid_skills ms
            JOIN skills s ON ms.skill_id = s.id
            WHERE ms.maid_id = ?
        ");
        $skillStmt->execute([$id]);
        $skills = $skillStmt->fetchAll();
        
        // Specialties
        $specStmt = $db->prepare("
            SELECT sp.name_en, sp.name_ar 
            FROM maid_specialties msp
            JOIN specialties sp ON msp.specialty_id = sp.id
            WHERE msp.maid_id = ?
        ");
        $specStmt->execute([$id]);
        $specialties = $specStmt->fetchAll();
        
        // Documents
        $docStmt = $db->prepare("
            SELECT md.id, md.file_path, md.expiry_date, md.status, md.admin_note,
                   dt.name_en as document_type_name
            FROM maid_documents md
            JOIN document_types dt ON md.document_type_id = dt.id
            WHERE md.maid_id = ?
            ORDER BY md.created_at DESC
        ");
        $docStmt->execute([$id]);
        $documents = $docStmt->fetchAll();
        
        // Calculate age
        $age = null;
        if (!empty($maid['date_of_birth'])) {
            $dob = new DateTime($maid['date_of_birth']);
            $now = new DateTime();
            $age = (int)$dob->diff($now)->y;
        }
        
        require __DIR__ . '/../../../../views/admin/maids/show.php';
    }

    public function approve() {
        $id = $_GET['id'] ?? 0;
        $maidModel = new Maid();
        $maidModel->update($id, [
            'verification_status' => 'approved',
            'status' => 'published'
        ]);
        header("Location: ?page=maids&success=Maid activated and approved successfully");
    }

    public function reject() {
        $id = $_GET['id'] ?? 0;
        $maidModel = new Maid();
        $maidModel->update($id, [
            'verification_status' => 'rejected',
            'status' => 'suspended'
        ]);
        header("Location: ?page=maids&success=Maid deactivated successfully");
        exit;
    }

    public function toggleRecommended() {
        $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
        if ($id) {
            $maidModel = new Maid();
            $maid = $maidModel->find($id);
            if ($maid) {
                $newRecommended = $maid['is_recommended'] ? 0 : 1;
                $maidModel->update($id, ['is_recommended' => $newRecommended]);
                header("Location: ?page=maids&success=Maid recommended status updated successfully");
                exit;
            }
        }
        header("Location: ?page=maids&error=Invalid maid ID");
        exit;
    }

    public function toggleVerified() {
        $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
        if ($id) {
            $maidModel = new Maid();
            $maid = $maidModel->find($id);
            if ($maid) {
                $newVerified = $maid['emirates_id_verified'] ? 0 : 1;
                $maidModel->update($id, ['emirates_id_verified' => $newVerified]);
                header("Location: ?page=maids&success=Maid verification / EID check status updated successfully");
                exit;
            }
        }
        header("Location: ?page=maids&error=Invalid maid ID");
        exit;
    }
}
