/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 */
package maindb

const schema = `
    CREATE TABLE IF NOT EXISTS files (
        id          VARCHAR(255) NOT NULL,
        collection  VARCHAR(255) NOT NULL,
        name        VARCHAR(255) NOT NULL,
        type        VARCHAR(255) NOT NULL,
        checksum    VARCHAR(255) NOT NULL,
        size        INTEGER,
        created_at  VARCHAR(255) NOT NULL,
        updated_at  VARCHAR(255) NOT NULL,
        created_by  VARCHAR(255) NOT NULL,
        updated_by  VARCHAR(255) NOT NULL,
        helm_hash   VARCHAR(255) NOT NULL,
        helm_meta   VARCHAR(4096) NOT NULL
    );
    CREATE UNIQUE INDEX IF NOT EXISTS files_index
        ON files(collection, name);

    --- DROP TABLE IF EXISTS manifests;
    CREATE TABLE IF NOT EXISTS manifests (
        id           VARCHAR(255) NOT NULL,
        name         VARCHAR(255) NOT NULL,
        reference    VARCHAR(255) NOT NULL,
        digest       VARCHAR(1024) NOT NULL,
        contentType  VARCHAR(255) NOT NULL,
        payload      VARCHAR(4096) NOT NULL,
        created_at   VARCHAR(255) NOT NULL,
        updated_at   VARCHAR(255) NOT NULL,
        created_by   VARCHAR(255) NOT NULL,
        updated_by   VARCHAR(255) NOT NULL,
        architecture VARCHAR(255) NOT NULL,
        os           VARCHAR(255) NOT NULL,
        variant      VARCHAR(255) NOT NULL
    );
    CREATE UNIQUE INDEX IF NOT EXISTS manifests_index01
        ON manifests(name, reference, architecture, os, variant);
    CREATE UNIQUE INDEX IF NOT EXISTS manifests_index02
        ON manifests(name, reference);


    CREATE TABLE IF NOT EXISTS blobs (
        id          VARCHAR(255) NOT NULL,
        name        VARCHAR(255) NOT NULL,
        reference   VARCHAR(255) NOT NULL,
        mediaType   VARCHAR(255) NOT NULL,
        digest      VARCHAR(255) NOT NULL,
        size        INTEGER NOT NULL,
        created_at  VARCHAR(255) NOT NULL,
        updated_at  VARCHAR(255) NOT NULL,
        created_by  VARCHAR(255) NOT NULL,
        updated_by  VARCHAR(255) NOT NULL
    );
    CREATE UNIQUE INDEX IF NOT EXISTS blobs_index
        ON blobs(name, reference, digest);

    --- DROP TABLE IF EXISTS accounts;
    CREATE TABLE IF NOT EXISTS accounts (
	id          TEXT NOT NULL,
	username    TEXT NOT NULL,
	passhash    TEXT NOT NULL,
        created_at  TEXT NOT NULL,
        updated_at  TEXT NOT NULL,
        created_by  TEXT NOT NULL,
        updated_by  TEXT NOT NULL,
        disabled    BOOL
    );
    CREATE UNIQUE INDEX IF NOT EXISTS accounts_index01
        ON accounts(id);
    CREATE UNIQUE INDEX IF NOT EXISTS accounts_index02
        ON accounts(username);

    --- DROP TABLE IF EXISTS grants;
    CREATE TABLE IF NOT EXISTS grants (
	id          TEXT NOT NULL,
        account_id  INT  NOT NULL,
	right       TEXT NOT NULL,
	pattern     TEXT NOT NULL,
        created_at  TEXT NOT NULL,
        updated_at  TEXT NOT NULL,
        created_by  TEXT NOT NULL,
        updated_by  TEXT NOT NULL
    );
    CREATE UNIQUE INDEX IF NOT EXISTS grants_index00
        ON grants(id);
    CREATE INDEX IF NOT EXISTS grants_index01
        ON grants(account_id);
    CREATE INDEX IF NOT EXISTS grants_index02
        ON grants(account_id, right);
    CREATE UNIQUE INDEX IF NOT EXISTS grants_index03
        ON grants(account_id, right, pattern);

`
