<?php

namespace App\Models;

use App\Utils\Database;
use PDO;

/**
 * MarkdownFileVO - 笔记文件视图对象
 * 扩展 MarkdownFile，添加分组名称等展示字段
 */
class MarkdownFileVO extends MarkdownFile
{
    private $db;

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

    /**
     * 获取笔记详情，包含分组名称
     * @param $id 笔记ID
     * @param bool $includeDeleted 是否包含已删除的笔记
     * @return array|false
     */
    public function findByIdWithGrouping($id, $includeDeleted = false)
    {
        $sql = "SELECT
                    mf.id,
                    mf.title,
                    mf.file_name as fileName,
                    mf.content,
                    mf.grouping_id as groupingId,
                    mf.created_at as createdAt,
                    mf.updated_at as updatedAt,
                    mf.is_deleted as isDeleted,
                    mf.deleted_at as deletedAt,
                    mf.deleted_by as deletedBy,
                    mf.is_private as isPrivate,
                    g.grouping as groupingName
                FROM markdown_file mf
                LEFT JOIN `grouping` g ON mf.grouping_id = g.id
                WHERE mf.id = ?";

        if (!$includeDeleted) {
            $sql .= " AND mf.is_deleted = 0";
        }

        $stmt = $this->db->prepare($sql);
        $stmt->execute([$id]);
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    /**
     * 获取所有笔记，包含分组名称
     * @return array
     */
    public function getAllWithGrouping()
    {
        $stmt = $this->db->prepare(
            "SELECT
                mf.id,
                mf.title,
                mf.file_name as fileName,
                mf.created_at as createdAt,
                mf.updated_at as updatedAt,
                mf.is_private as isPrivate,
                mf.grouping_id as groupingId,
                g.grouping as groupingName
            FROM markdown_file mf
            LEFT JOIN `grouping` g ON mf.grouping_id = g.id
            WHERE mf.is_deleted = 0
            ORDER BY mf.updated_at DESC"
        );
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * 按分组ID获取笔记，包含分组名称
     * @param $groupingId 分组ID
     * @return array
     */
    public function getByGroupingIdWithGrouping($groupingId)
    {
        $stmt = $this->db->prepare(
            "SELECT
                mf.id,
                mf.title,
                mf.file_name as fileName,
                mf.created_at as createdAt,
                mf.updated_at as updatedAt,
                mf.is_private as isPrivate,
                mf.grouping_id as groupingId,
                g.grouping as groupingName
            FROM markdown_file mf
            LEFT JOIN `grouping` g ON mf.grouping_id = g.id
            WHERE mf.grouping_id = ? AND mf.is_deleted = 0
            ORDER BY mf.updated_at DESC"
        );
        $stmt->execute([$groupingId]);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * 搜索笔记，包含分组名称
     * @param $keyword 搜索关键词
     * @return array
     */
    public function searchByTitleWithGrouping($keyword)
    {
        $stmt = $this->db->prepare(
            "SELECT
                mf.id,
                mf.title,
                mf.file_name as fileName,
                mf.created_at as createdAt,
                mf.updated_at as updatedAt,
                mf.is_private as isPrivate,
                mf.grouping_id as groupingId,
                g.grouping as groupingName
            FROM markdown_file mf
            LEFT JOIN `grouping` g ON mf.grouping_id = g.id
            WHERE mf.title LIKE ? AND mf.is_deleted = 0
            ORDER BY mf.updated_at DESC"
        );
        $stmt->execute(['%' . $keyword . '%']);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * 获取最近的笔记，包含分组名称
     * @param int $limit 限制数量
     * @return array
     */
    public function getRecentWithGrouping($limit = 12)
    {
        $stmt = $this->db->prepare(
            "SELECT
                mf.id,
                mf.title,
                mf.file_name as fileName,
                mf.created_at as createdAt,
                mf.updated_at as updatedAt,
                mf.is_private as isPrivate,
                mf.grouping_id as groupingId,
                g.grouping as groupingName
            FROM markdown_file mf
            LEFT JOIN `grouping` g ON mf.grouping_id = g.id
            WHERE mf.is_deleted = 0
            ORDER BY mf.updated_at DESC
            LIMIT ?"
        );
        $stmt->execute([$limit]);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
