<?php

require_once BASE_PATH
    . '/config/database.php';


class SocialLink
{
    private PDO $db;


    public function __construct()
    {
        $database =
            new Database();

        $this->db =
            $database->connect();
    }


    // ========================================================
    // GET ALL
    // ========================================================

    public function getAll(): array
    {
        $stmt =
            $this->db->query("
                SELECT *
                FROM social_links

                ORDER BY
                    sort_order ASC,
                    platform ASC
            ");


        return $stmt->fetchAll();
    }


    // ========================================================
    // GET ACTIVE
    // ========================================================

    public function getActive(): array
    {
        $stmt =
            $this->db->query("
                SELECT *
                FROM social_links

                WHERE is_active = 1

                ORDER BY
                    sort_order ASC,
                    platform ASC
            ");


        return $stmt->fetchAll();
    }


    // ========================================================
    // FIND BY ID
    // ========================================================

    public function findById(
        int $id
    ): ?array {

        $stmt =
            $this->db->prepare("
                SELECT *
                FROM social_links

                WHERE id = :id

                LIMIT 1
            ");


        $stmt->execute([
            'id' => $id
        ]);


        $social =
            $stmt->fetch();


        return $social ?: null;
    }


    // ========================================================
    // FIND BY PLATFORM
    // ========================================================

    public function findByPlatform(
        string $platform
    ): ?array {

        $stmt =
            $this->db->prepare("
                SELECT *
                FROM social_links

                WHERE platform = :platform

                LIMIT 1
            ");


        $stmt->execute([
            'platform' =>
                strtolower(
                    trim($platform)
                )
        ]);


        $social =
            $stmt->fetch();


        return $social ?: null;
    }


    // ========================================================
    // CREATE OR UPDATE
    // ========================================================

    public function save(
        array $data
    ): int {

        $existing =
            $this->findByPlatform(
                $data['platform']
            );


        // Nếu đã tồn tại platform → update
        if ($existing) {

            $stmt =
                $this->db->prepare("
                    UPDATE social_links

                    SET
                        url = :url,
                        sort_order = :sort_order,
                        is_active = :is_active

                    WHERE id = :id
                ");


            $stmt->execute([
                'url' =>
                    $data['url'],

                'sort_order' =>
                    $data['sort_order'],

                'is_active' =>
                    $data['is_active'],

                'id' =>
                    $existing['id']
            ]);


            return (int)
                $existing['id'];
        }


        // Chưa tồn tại → insert
        $stmt =
            $this->db->prepare("
                INSERT INTO social_links (
                    platform,
                    url,
                    sort_order,
                    is_active
                )
                VALUES (
                    :platform,
                    :url,
                    :sort_order,
                    :is_active
                )
            ");


        $stmt->execute([
            'platform' =>
                strtolower(
                    trim(
                        $data['platform']
                    )
                ),

            'url' =>
                $data['url'],

            'sort_order' =>
                $data['sort_order'],

            'is_active' =>
                $data['is_active']
        ]);


        return (int)
            $this->db->lastInsertId();
    }


    // ========================================================
    // TOGGLE ACTIVE
    // ========================================================

    public function toggle(
        int $id
    ): void {

        $stmt =
            $this->db->prepare("
                UPDATE social_links

                SET is_active =
                    CASE
                        WHEN is_active = 1
                        THEN 0
                        ELSE 1
                    END

                WHERE id = :id
            ");


        $stmt->execute([
            'id' => $id
        ]);
    }


    // ========================================================
    // DELETE
    // ========================================================

    public function delete(
        int $id
    ): void {

        $stmt =
            $this->db->prepare("
                DELETE FROM social_links

                WHERE id = :id
            ");


        $stmt->execute([
            'id' => $id
        ]);
    }
}