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

class InterviewController {
    public function index() {
        $interviewModel = new Interview();
        
        $page = isset($_GET['p']) ? (int)$_GET['p'] : 1;
        $perPage = 10;
        
        $searchTerm = $_GET['search'] ?? '';
        $status = $_GET['status'] ?? '';
        $date = $_GET['date'] ?? '';
        
        $filters = [];
        if ($status !== '') $filters['status'] = $status;
        if ($date !== '') $filters['date'] = $date;
        
        $result = $interviewModel->searchAndPaginateWithDetails($page, $perPage, $searchTerm, $filters);
        $interviews = $result['data'];
        
        $total = $result['total'];
        $totalPages = ceil($total / $perPage);

        require __DIR__ . '/../../../../views/admin/interviews/index.php';
    }

    public function updateStatus() {
        $id = $_GET['id'] ?? 0;
        $status = $_GET['status'] ?? '';
        
        if ($id && $status) {
            $interviewModel = new Interview();
            $interviewModel->update($id, ['status' => $status]);
            $this->logAction("Update Status: " . $status, $id);
        }
        
        header("Location: ?page=interviews&success=Status updated");
    }

    public function delete() {
        $id = $_GET['id'] ?? 0;
        if ($id) {
            $interviewModel = new Interview();
            $interviewModel->delete($id);
            $this->logAction("Delete Interview", $id);
        }
        header("Location: ?page=interviews&success=Interview deleted");
    }

    private function logAction($action, $targetId = null, $details = null) {
        require_once __DIR__ . '/../../../Models/AuditLog.php';
        (new AuditLog())->log($action, 'interviews', $targetId, $details);
    }
}
