<?php
class SAPlansController {
    public function index(): void {
        SuperAuth::requireLogin();
        $plans = db()->query("SELECT * FROM plans")->fetchAll();
        $pageTitle = 'Subscription Plans';
        ob_start();
        include APP_ROOT . '/views/superadmin/plans/index.php';
        $content = ob_get_clean();
        include APP_ROOT . '/views/superadmin/layout.php';
    }

    public function create(): void {
        SuperAuth::requireLogin();
        $pageTitle = 'Create Plan';
        ob_start();
        include APP_ROOT . '/views/superadmin/plans/create.php';
        $content = ob_get_clean();
        include APP_ROOT . '/views/superadmin/layout.php';
    }

    public function store(): void {
        SuperAuth::requireLogin();
        Security::checkCsrf();

        $data = [
            'name'           => Security::sanitize($_POST['name']),
            'slug'           => Security::makeSlug($_POST['name']),
            'price_monthly'  => (float)$_POST['price_monthly'],
            'price_yearly'   => (float)$_POST['price_yearly'],
            'max_products'   => (int)$_POST['max_products'],
            'max_users'      => (int)$_POST['max_users'],
            'max_locations'  => (int)$_POST['max_locations'],
            'storage_mb'     => (int)$_POST['storage_mb'],
            'features'       => json_encode($_POST['features'] ?? [])
        ];

        $cols = implode(',', array_map(fn($k) => "`$k`", array_keys($data)));
        $vals = implode(',', array_fill(0, count($data), '?'));
        db()->prepare("INSERT INTO plans ($cols) VALUES ($vals)")->execute(array_values($data));

        flash('success', 'Plan created successfully');
        redirect('superadmin/plans');
    }

    public function edit(string $id): void {
        SuperAuth::requireLogin();

        $plan = db()->prepare("SELECT * FROM plans WHERE id=?");
        $plan->execute([$id]);
        $plan = $plan->fetch();

        if (!$plan) {
            flash('error', 'Plan not found');
            redirect('superadmin/plans');
        }

        $pageTitle = 'Edit Plan';
        ob_start();
        include APP_ROOT . '/views/superadmin/plans/edit.php';
        $content = ob_get_clean();
        include APP_ROOT . '/views/superadmin/layout.php';
    }

    public function update(string $id): void {
        SuperAuth::requireLogin();
        Security::checkCsrf();

        $data = [
            'name'           => Security::sanitize($_POST['name']),
            'slug'           => Security::makeSlug($_POST['name']),
            'price_monthly'  => (float)$_POST['price_monthly'],
            'price_yearly'   => (float)$_POST['price_yearly'],
            'max_products'   => (int)$_POST['max_products'],
            'max_users'      => (int)$_POST['max_users'],
            'max_locations'  => (int)$_POST['max_locations'],
            'storage_mb'     => (int)$_POST['storage_mb'],
            'features'       => json_encode($_POST['features'] ?? [])
        ];

        $set = implode(',', array_map(fn($k) => "`$k`=?", array_keys($data)));
        db()->prepare("UPDATE plans SET $set WHERE id=?")->execute([...array_values($data), $id]);

        flash('success', 'Plan updated successfully');
        redirect('superadmin/plans');
    }
}
