<?php
// app/controllers/TeamController.php

// Define App Root for cleaner imports
$appRoot = __DIR__ . '/..';
require_once $appRoot . '/models/User.php';

class TeamController {
    private $pdo;
    private $userModel;

    public function __construct($pdo) {
        $this->pdo = $pdo;
        $this->userModel = new User($pdo);
    }

    // 1. List all Team Members
    public function index() {
        // Double check User model has getAll
        if (!method_exists($this->userModel, 'getAll')) {
            die("Error: User model is missing getAll() method.");
        }
        
        $users = $this->userModel->getAll();
        $pageTitle = "Team Management";
        $viewsPath = __DIR__ . '/../views';
        
        require_once $viewsPath . '/layouts/header.php';
        require_once $viewsPath . '/team/index.php';
        require_once $viewsPath . '/layouts/footer.php';
    }

    // 2. Add a New Team Member
    public function store() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $password = $_POST['password'];
            $role = $_POST['role'];

            // Validate User Model methods exist
            if (!method_exists($this->userModel, 'findByEmail')) {
                die("Error: User model is missing findByEmail() method. Please update User.php");
            }

            // Check if email already exists
            if ($this->userModel->findByEmail($email)) {
                echo "<script>alert('User with this email already exists!'); window.location='/team.php';</script>";
                exit;
            }

            // Create User
            if ($this->userModel->create($name, $email, $password, $role)) {
                header('Location: /team.php');
                exit;
            } else {
                // If it fails, show why
                echo "Error adding team member. Database connection might be interrupted.";
                exit;
            }
        }
    }

    // 3. Show Edit Form
    public function edit($id) {
        $user = $this->userModel->findById($id);
        
        if (!$user) {
            header('Location: /team.php');
            exit;
        }

        $pageTitle = "Edit Member";
        $viewsPath = __DIR__ . '/../views';

        require_once $viewsPath . '/layouts/header.php';
        require_once $viewsPath . '/team/edit.php';
        require_once $viewsPath . '/layouts/footer.php';
    }

    // 4. Update Existing Member
    public function update() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $id = $_POST['id'];
            $name = $_POST['name'];
            $email = $_POST['email'];
            $role = $_POST['role'];

            if ($this->userModel->update($id, $name, $email, $role)) {
                header('Location: /team.php');
                exit;
            } else {
                echo "Error updating user.";
            }
        }
    }

    // 5. Delete a Member
    public function delete($id) {
        if ($id == $_SESSION['user_id']) {
            echo "<script>alert('You cannot delete your own account from here!'); window.location='/team.php';</script>";
            exit;
        }
        
        if ($this->userModel->delete($id)) {
            header('Location: /team.php');
            exit;
        } else {
            echo "Error deleting user.";
        }
    }
}
?>