<?php

namespace App\Controllers;

use App\Models\User;
use App\Models\Admin;
use App\Models\Pegawai;
use App\Models\Staff;
use App\Models\Pimpinan;
use App\Models\Humas;
use App\Models\Mahasiswa;
use App\Models\Dosen;
use App\Models\Kaprodi;

class AuthController
{
    public function login()
    {
        $username = $_POST['username'] ?? '';
        $password = md5($_POST['password'] ?? '');

        $userModel = new User();
        $user = $userModel->findByUsername($username);

        if (!$user || $password !== $user['password']) {
            $_SESSION['error'] = 'Username atau password salah.';
            header('Location: /');
            exit;
        }

        // Ambil data tambahan sesuai level user
        switch (strtolower($user['level'])) {
            case 'admin':
                $model = new Admin();
                $detail = $model->findByUsername($username);
                if (!$detail) {
                    $_SESSION['error'] = 'Data admin tidak ditemukan.';
                    header('Location: /');
                    exit;
                }
                $user = array_merge($user, $detail);
                break;

            case 'pegawai':
                $model = new Pegawai();
                $detail = $model->findByUsername($username);
                if (!$detail) {
                    $_SESSION['error'] = 'Data pegawai tidak ditemukan.';
                    header('Location: /');
                    exit;
                }
                $user = array_merge($user, $detail);
                break;

            case 'staff':
                $model = new Staff();
                $detail = $model->findByUsername($username);
                if (!$detail) {
                    $_SESSION['error'] = 'Data staff tidak ditemukan.';
                    header('Location: /');
                    exit;
                }
                $user = array_merge($user, $detail);
                break;

            case 'pimpinan':
                $model = new Pimpinan();
                $detail = $model->findByUsername($username);
                if (!$detail) {
                    $_SESSION['error'] = 'Data pimpinan tidak ditemukan.';
                    header('Location: /');
                    exit;
                }
                $user = array_merge($user, $detail);
                break;

            case 'humas':
                $model = new Humas();
                $detail = $model->findByUsername($username);
                if (!$detail) {
                    $_SESSION['error'] = 'Data humas tidak ditemukan.';
                    header('Location: /');
                    exit;
                }
                $user = array_merge($user, $detail);
                break;

            case 'mahasiswa':
                $model = new Mahasiswa();
                $detail = $model->findByUsername($username);
                if (!$detail) {
                    $_SESSION['error'] = 'Data mahasiswa tidak ditemukan.';
                    header('Location: /');
                    exit;
                }
                $user = array_merge($user, $detail);
                break;

            case 'dosen':
                $model = new Dosen();
                $detail = $model->findByUsername($username);
                if (!$detail) {
                    $_SESSION['error'] = 'Data dosen tidak ditemukan.';
                    header('Location: /');
                    exit;
                }
                $user = array_merge($user, $detail);
                break;

            case 'dosen/kaprodi':
                $model = new Kaprodi();
                $detail = $model->findByUsername($username);
                if (!$detail) {
                    $_SESSION['error'] = 'Data kaprodi tidak ditemukan.';
                    header('Location: /');
                    exit;
                }
                $user = array_merge($user, $detail);
                break;

            default:
                // Tidak ada data tambahan, lanjut saja
                break;
        }

        $_SESSION['user'] = $user;

        // Redirect sesuai level
        switch (strtolower($user['level'])) {
            case 'admin':
                header('Location: /dashboard/admin');
                break;
            case 'pegawai':
                header('Location: /dashboard/pegawai');
                break;
            case 'staff':
                header('Location: /dashboard/staff');
                break;
            case 'pimpinan':
                header('Location: /dashboard/pimpinan');
                break;
            case 'humas':
                header('Location: /dashboard/humas');
                break;
            case 'mahasiswa':
                header('Location: /dashboard/mahasiswa');
                break;
            case 'dosen':
                header('Location: /dashboard/dosen');
                break;
            case 'dosen/kaprodi':
                header('Location: /dashboard/kaprodi');
                break;
            default:
                $_SESSION['error'] = 'Level user tidak dikenali.';
                header('Location: /');
                break;
        }

        exit;
    }

    public function logout()
    {
        session_destroy();
        header('Location: /');
        exit;
    }
}
