<?php

namespace Core\App\Controllers;

use Core\App\Models\Footer;
use Core\App\Models\FooterLink;

class FooterController
{
    private int $maxLinksPerSection = 5;

    /**
     * الأنواع المسموحة حاليًا
     * (branding سنضيفه لاحقًا بدون كسر)
     */
    private array $allowedSectionTypes = [
        'pages',
        'contact',
        'social',
        // 'branding' ← لاحقًا
    ];

    public function index()
    {
        $sections = (new Footer())->getAll();
        include ROOT_PATH . '/admin/templates/footer/index.php';
    }

    public function create()
    {
        include ROOT_PATH . '/admin/templates/footer/create.php';
    }

    public function store()
    {
        $data = $_POST;

        if (isset($data['section_type']) && !in_array($data['section_type'], $this->allowedSectionTypes, true)) {
            $data['section_type'] = 'pages';
        }

        (new Footer())->create($data);

        header('Location: /admin/footer');
        exit;
    }

    public function edit($id)
    {
        $footerModel = new Footer();
        $section = $footerModel->find((int)$id);
        $links   = $footerModel->getLinks((int)$id);

        include ROOT_PATH . '/admin/templates/footer/edit.php';
    }

    public function update($id)
    {
        $data = $_POST;

        if (isset($data['section_type']) && !in_array($data['section_type'], $this->allowedSectionTypes, true)) {
            $data['section_type'] = 'pages';
        }

        (new Footer())->update((int)$id, $data);

        header('Location: /admin/footer');
        exit;
    }

    public function delete($id)
    {
        (new Footer())->delete((int)$id);

        header('Location: /admin/footer');
        exit;
    }

    public function storeLink($sectionId)
    {
        $footerModel = new Footer();
        $links = $footerModel->getLinks((int)$sectionId);

        if (count($links) >= $this->maxLinksPerSection) {
            header('Location: /admin/footer/edit/' . (int)$sectionId . '?error=limit');
            exit;
        }

        (new FooterLink())->create((int)$sectionId, $_POST);

        header('Location: /admin/footer/edit/' . (int)$sectionId);
        exit;
    }

    public function updateLink($id)
    {
        (new FooterLink())->update((int)$id, $_POST);

        \Core\System\Response::back('/admin/footer');
    }

    public function deleteLink($id)
    {
        \Core\System\Csrf::check();
        (new FooterLink())->delete((int)$id);

        \Core\System\Response::back('/admin/footer');
    }

    /**
     * =========================
     * NEW: Save footer sections order (Drag & Drop)
     * =========================
     */
    public function reorder()
    {
        header('Content-Type: application/json');

        $raw = file_get_contents('php://input');
        $payload = json_decode($raw, true);

        if (!is_array($payload) || !isset($payload['order']) || !is_array($payload['order'])) {
            http_response_code(400);
            echo json_encode(['success' => false]);
            return;
        }

        $footerModel = new Footer();

        foreach ($payload['order'] as $item) {
            if (!isset($item['id'], $item['position'])) {
                continue;
            }

            $footerModel->update(
                (int)$item['id'],
                ['position' => (int)$item['position']]
            );
        }

        echo json_encode(['success' => true]);
    }
}
