CREATE TABLE public.templates (
    id                    uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    cluster_id            uuid NOT NULL REFERENCES public.clusters(id) ON DELETE CASCADE,
    name                  text NOT NULL,
    description           text NOT NULL DEFAULT '',
    proxmox_template_vmid int  NOT NULL,
    proxmox_node          text NOT NULL DEFAULT '',
    created_at            timestamptz NOT NULL DEFAULT now(),
    updated_at            timestamptz,
    CONSTRAINT templates_name_not_blank_check CHECK (btrim(name) <> ''),
    CONSTRAINT templates_proxmox_vmid_positive_check CHECK (proxmox_template_vmid > 0),
    CONSTRAINT templates_cluster_node_vmid_unique UNIQUE (cluster_id, proxmox_node, proxmox_template_vmid)
);

CREATE INDEX templates_cluster_id_idx ON public.templates (cluster_id);