<?php

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


class ShopSetting
{
    private PDO $db;


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

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


    // ========================================================
    // GET SETTINGS
    // ========================================================

    public function get(): array
    {
        $stmt =
            $this->db->prepare("
                SELECT *
                FROM shop_settings
                WHERE id = 1
                LIMIT 1
            ");

        $stmt->execute();

        $settings =
            $stmt->fetch();


        // Trường hợp database chưa có row id = 1
        if (!$settings) {

            $this->createDefault();

            $stmt->execute();

            $settings =
                $stmt->fetch();
        }


        return $settings ?: [
            'id' => 1,
            'shop_name' => 'PET SHOP',
            'logo' => null,
            'phone' => null,
            'email' => null,
            'address' => null,
            'description' => null,
            'map_embed_url' => null,
            'shipping_fee' => 0,
            'free_shipping_from' => null,
            'currency' => 'VND'
        ];
    }


    // ========================================================
    // CREATE DEFAULT
    // ========================================================

    private function createDefault(): void
    {
        $stmt =
            $this->db->prepare("
                INSERT IGNORE INTO shop_settings (
                    id,
                    shop_name,
                    shipping_fee,
                    currency
                )
                VALUES (
                    1,
                    'PET SHOP',
                    0,
                    'VND'
                )
            ");

        $stmt->execute();
    }


    // ========================================================
    // UPDATE
    // ========================================================

    public function update(
        array $data
    ): void {

        $this->createDefault();


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

                SET
                    shop_name = :shop_name,
                    phone = :phone,
                    email = :email,
                    address = :address,
                    description = :description,
                    map_embed_url = :map_embed_url,
                    shipping_fee = :shipping_fee,
                    free_shipping_from = :free_shipping_from,
                    currency = :currency

                WHERE id = 1
            ");


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

            'phone' =>
                $data['phone'] ?: null,

            'email' =>
                $data['email'] ?: null,

            'address' =>
                $data['address'] ?: null,

            'description' =>
                $data['description'] ?: null,

            'map_embed_url' =>
                $data['map_embed_url'] ?: null,

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

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

            'currency' =>
                $data['currency']
        ]);
    }

    /** Update information that belongs to PET SPA, leaving retired sales fields intact. */
    public function updateSpaDetails(array $data): void
    {
        $this->createDefault();
        $stmt = $this->db->prepare(
            'UPDATE shop_settings SET shop_name = :shop_name, phone = :phone,
             email = :email, address = :address, description = :description,
             map_embed_url = :map_embed_url WHERE id = 1'
        );
        $stmt->execute([
            'shop_name' => $data['shop_name'], 'phone' => $data['phone'] ?: null,
            'email' => $data['email'] ?: null, 'address' => $data['address'] ?: null,
            'description' => $data['description'] ?: null,
            'map_embed_url' => $data['map_embed_url'] ?: null
        ]);
    }


    // ========================================================
    // LOGO
    // ========================================================

    public function updateLogo(
        ?string $logo
    ): void {

        $this->createDefault();


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

                SET logo = :logo

                WHERE id = 1
            ");


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