<?php

class FunnelModel {
    private $db;

    public function __construct() {
        $this->db = Database::getInstance();
    }

    public function getAll() {
        return $this->db->fetchAll("
            SELECT f.*, COUNT(fp.id) as page_count
            FROM funnels f
            LEFT JOIN funnel_pages fp ON fp.funnel_id = f.id
            GROUP BY f.id
            ORDER BY f.created_at DESC
        ");
    }

    public function getById($id) {
        return $this->db->fetch("SELECT * FROM funnels WHERE id=?", [$id]);
    }

    public function getBySlug($slug) {
        return $this->db->fetch("SELECT * FROM funnels WHERE slug=?", [$slug]);
    }

    public function create($data) {
        return $this->db->insert("
            INSERT INTO funnels (name, description, slug, created_at, updated_at)
            VALUES (?, ?, ?, NOW(), NOW())
        ", [
            $data['name'],
            $data['description'] ?? '',
            $data['slug'],
        ]);
    }

    public function update($id, $data) {
        return $this->db->execute("
            UPDATE funnels SET name=?, description=?, updated_at=NOW() WHERE id=?
        ", [$data['name'], $data['description'] ?? '', $id]);
    }

    public function delete($id) {
        $pages = $this->getPagesById($id);
        foreach ($pages as $page) {
            $this->deletePage($page['id']);
        }
        return $this->db->execute("DELETE FROM funnels WHERE id=?", [$id]);
    }

    // Pages
    public function getPagesById($funnelId) {
        return $this->db->fetchAll("
            SELECT * FROM funnel_pages WHERE funnel_id=? ORDER BY step_order ASC
        ", [$funnelId]);
    }

    public function getPageById($id) {
        return $this->db->fetch("SELECT * FROM funnel_pages WHERE id=?", [$id]);
    }

    public function getFirstPage($funnelId) {
        return $this->db->fetch("
            SELECT * FROM funnel_pages WHERE funnel_id=? ORDER BY step_order ASC LIMIT 1
        ", [$funnelId]);
    }

    public function createPage($data) {
        $maxOrder = $this->db->fetch("SELECT MAX(step_order) as mo FROM funnel_pages WHERE funnel_id=?", [$data['funnel_id']]);
        $order = ($maxOrder['mo'] ?? 0) + 1;

        return $this->db->insert("
            INSERT INTO funnel_pages (funnel_id, title, content, form_id, step_order, page_type, settings, created_at)
            VALUES (?, ?, ?, ?, ?, ?, ?, NOW())
        ", [
            $data['funnel_id'],
            $data['title'],
            $data['content'] ?? '',
            $data['form_id'] ?: null,
            $order,
            $data['page_type'] ?? 'landing',
            $data['settings'] ?? '{}',
        ]);
    }

    public function updatePage($id, $data) {
        return $this->db->execute("
            UPDATE funnel_pages SET title=?, content=?, form_id=?, page_type=?, settings=?
            WHERE id=?
        ", [
            $data['title'],
            $data['content'] ?? '',
            $data['form_id'] ?: null,
            $data['page_type'] ?? 'landing',
            $data['settings'] ?? '{}',
            $id,
        ]);
    }

    public function deletePage($id) {
        return $this->db->execute("DELETE FROM funnel_pages WHERE id=?", [$id]);
    }

    public function reorderPages($funnelId, $order) {
        foreach ($order as $index => $pageId) {
            $this->db->execute("UPDATE funnel_pages SET step_order=? WHERE id=? AND funnel_id=?", [$index + 1, $pageId, $funnelId]);
        }
    }
}
