<?php

namespace SHServ\Models;

class Modes extends \SHServ\Middleware\Model {

    public static $table_name = "shserv_modes";

    /**
     * Ensure tags from ModesRegistry exist as rows in DB.
     * Lazy sync: inserts missing tags with is_active=0.
     */
    public function sync_registry_tags(): void {
        $definitions = \ControlScripts\ModesRegistry::definitions();

        foreach(array_keys($definitions) as $tag) {
            $exists = $this -> thin_builder() -> select(
                self::$table_name,
                [ "tag" ],
                [ [ "tag", "=", $tag ] ]
            );

            if(!$exists) {
                $this -> thin_builder() -> insert(
                    self::$table_name,
                    [ "tag" => $tag, "is_active" => 0 ]
                );
            }
        }
    }

    public function enable(String $tag): bool {
        return $this -> set_state($tag, 1);
    }

    public function disable(String $tag): bool {
        return $this -> set_state($tag, 0);
    }

    protected function set_state(String $tag, int $state): bool {
        $result = $this -> thin_builder() -> update(
            self::$table_name,
            [ "is_active" => $state ],
            [ [ "tag", "=", $tag ] ]
        );

        return (bool) $result;
    }

    public function is_active(String $tag): bool {
        $result = $this -> thin_builder() -> select(
            self::$table_name,
            [ "is_active" ],
            [ [ "tag", "=", $tag ] ]
        );

        return !empty($result) && (int) $result[0]["is_active"] === 1;
    }

    public function any_active(Array $tags): bool {
        if(empty($tags)) {
            return false;
        }

        $result = $this -> thin_builder() -> select(
            self::$table_name,
            [ "tag" ],
            [
                [ "tag", "IN", $tags ],
                "AND",
                [ "is_active", "=", 1 ]
            ]
        );

        return !empty($result);
    }

    public function all_active(Array $tags): bool {
        if(empty($tags)) {
            return false;
        }

        $result = $this -> thin_builder() -> select(
            self::$table_name,
            [ "tag" ],
            [
                [ "tag", "IN", $tags ],
                "AND",
                [ "is_active", "=", 1 ]
            ]
        );

        return count($result) === count($tags);
    }

    public function active_tags(): Array {
        $result = $this -> thin_builder() -> select(
            self::$table_name,
            [ "tag" ],
            [ [ "is_active", "=", 1 ] ]
        );

        return array_column($result ?: [], "tag");
    }

    public function list_all(): Array {
        $rows = $this -> thin_builder() -> select(
            self::$table_name,
            [],
            [],
            [ "tag" ],
            "ASC"
        );

        $definitions = \ControlScripts\ModesRegistry::definitions();
        $out = [];

        foreach($rows ?: [] as $row) {
            $tag = $row["tag"];
            $meta = $definitions[$tag] ?? [ "label" => $tag, "description" => "" ];

            $out[] = [
                "tag" => $tag,
                "label" => $meta["label"],
                "description" => $meta["description"] ?? "",
                "is_active" => (bool) $row["is_active"],
                "updated_at" => $row["updated_at"]
            ];
        }

        return $out;
    }
}
