<?php

class FormController {
    private $formModel;
    private $fieldModel;

    public function __construct() {
        $this->formModel = new FormModel();
        $this->fieldModel = new FieldModel();
    }

    public function index() {
        $forms = $this->formModel->getAll();
        require_once APP_PATH . '/views/forms/index.php';
    }

    public function create() {
        require_once APP_PATH . '/views/forms/create.php';
    }

    public function store() {
        if ($_SERVER['REQUEST_METHOD'] !== 'POST') { redirect('/forms'); }

        $name = sanitize($_POST['name'] ?? '');
        if (empty($name)) {
            $_SESSION['error'] = 'Form name is required.';
            redirect('/forms/create');
        }

        $slug = generateSlug($name);
        $id = $this->formModel->create([
            'name'              => $name,
            'description'       => sanitize($_POST['description'] ?? ''),
            'slug'              => $slug,
            'whatsapp_number'   => sanitize($_POST['whatsapp_number'] ?? ''),
            'thank_you_message' => sanitize($_POST['thank_you_message'] ?? 'Thank you for your submission!'),
            'redirect_url'      => sanitize($_POST['redirect_url'] ?? ''),
        ]);

        $_SESSION['success'] = 'Form created successfully!';
        redirect('/forms/' . $id . '/edit');
    }

    public function edit($id) {
        $form = $this->formModel->getById($id);
        if (!$form) { redirect('/forms'); }
        $fields = $this->fieldModel->getByFormId($id);
        require_once APP_PATH . '/views/forms/edit.php';
    }

    public function update($id) {
        $form = $this->formModel->getById($id);
        if (!$form) { redirect('/forms'); }

        $name = sanitize($_POST['name'] ?? '');
        if (empty($name)) {
            $_SESSION['error'] = 'Form name is required.';
            redirect('/forms/' . $id . '/edit');
        }

        $this->formModel->update($id, [
            'name'              => $name,
            'description'       => sanitize($_POST['description'] ?? ''),
            'whatsapp_number'   => sanitize($_POST['whatsapp_number'] ?? ''),
            'thank_you_message' => sanitize($_POST['thank_you_message'] ?? 'Thank you!'),
            'redirect_url'      => sanitize($_POST['redirect_url'] ?? ''),
        ]);

        $_SESSION['success'] = 'Form updated successfully!';
        redirect('/forms/' . $id . '/edit');
    }

    public function delete($id) {
        $form = $this->formModel->getById($id);
        if (!$form) {
            jsonResponse(['success' => false, 'message' => 'Form not found']);
        }

        // Delete related fields & responses
        $db = Database::getInstance();
        $db->execute("DELETE FROM response_values WHERE response_id IN (SELECT id FROM responses WHERE form_id=?)", [$id]);
        $db->execute("DELETE FROM responses WHERE form_id=?", [$id]);
        $db->execute("DELETE FROM fields WHERE form_id=?", [$id]);
        $this->formModel->delete($id);

        if (isAjax()) {
            jsonResponse(['success' => true, 'message' => 'Form deleted.']);
        }
        $_SESSION['success'] = 'Form deleted.';
        redirect('/forms');
    }
}
