BEGIN;

CREATE TABLE IF NOT EXISTS public.report_customer_config (
    customer_id      VARCHAR(64)  PRIMARY KEY,
    customer_name    VARCHAR(160) NOT NULL,
    property_name    VARCHAR(160) NOT NULL,
    location         VARCHAR(160) NOT NULL,
    contact_line     VARCHAR(180) NOT NULL DEFAULT '',
    logo_data_uri    TEXT,
    logo_text        VARCHAR(5)   NOT NULL,
    theme            VARCHAR(40)  NOT NULL DEFAULT 'modern',
    primary_color    CHAR(7)      NOT NULL DEFAULT '#1F5D66',
    secondary_color  CHAR(7)      NOT NULL DEFAULT '#2C7C82',
    accent_color     CHAR(7)      NOT NULL DEFAULT '#DBA25D',
    text_color       CHAR(7)      NOT NULL DEFAULT '#24343A',
    muted_color      CHAR(7)      NOT NULL DEFAULT '#718087',
    surface_color    CHAR(7)      NOT NULL DEFAULT '#F2F6F5',
    font_family      VARCHAR(100) NOT NULL DEFAULT 'Arial',
    active           BOOLEAN      NOT NULL DEFAULT TRUE,
    created_at       TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
    updated_at       TIMESTAMPTZ  NOT NULL DEFAULT NOW()
);

-- Upgrade-Pfad für die ursprüngliche Repo-Struktur.
ALTER TABLE public.report_customer_config
    ALTER COLUMN theme TYPE VARCHAR(40);

ALTER TABLE public.report_customer_config
    DROP CONSTRAINT IF EXISTS report_customer_config_theme_check;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'report_customer_config_theme_not_empty'
          AND conrelid = 'public.report_customer_config'::regclass
    ) THEN
        ALTER TABLE public.report_customer_config
            ADD CONSTRAINT report_customer_config_theme_not_empty
            CHECK (length(trim(theme)) > 0) NOT VALID;
    END IF;
END;
$$;

ALTER TABLE public.report_customer_config
    VALIDATE CONSTRAINT report_customer_config_theme_not_empty;

CREATE TABLE IF NOT EXISTS public.monthly_report_runs (
    id                BIGSERIAL   PRIMARY KEY,
    customer_id       VARCHAR(64) NOT NULL
                      REFERENCES public.report_customer_config(customer_id),
    report_year       INTEGER     NOT NULL
                      CHECK (report_year BETWEEN 2020 AND 2100),
    report_month      INTEGER     NOT NULL
                      CHECK (report_month BETWEEN 1 AND 12),
    status            VARCHAR(20) NOT NULL
                      CHECK (status IN ('queued', 'processing', 'completed', 'failed')),
    report_version    VARCHAR(20) NOT NULL,
    prompt_version    VARCHAR(20) NOT NULL,
    template_version  VARCHAR(20) NOT NULL,
    claude_model      VARCHAR(80),
    storage_path      TEXT,
    checksum_sha256   CHAR(64),
    error_message     TEXT,
    started_at        TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    completed_at      TIMESTAMPTZ,
    created_at        TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    UNIQUE (customer_id, report_year, report_month, report_version)
);

CREATE INDEX IF NOT EXISTS idx_monthly_report_runs_status
    ON public.monthly_report_runs(status, report_year, report_month);

CREATE INDEX IF NOT EXISTS idx_monthly_report_runs_customer_period
    ON public.monthly_report_runs(customer_id, report_year, report_month);

CREATE OR REPLACE FUNCTION public.set_report_customer_config_updated_at()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$;

DROP TRIGGER IF EXISTS trg_report_customer_config_updated_at
    ON public.report_customer_config;

CREATE TRIGGER trg_report_customer_config_updated_at
BEFORE UPDATE ON public.report_customer_config
FOR EACH ROW
EXECUTE FUNCTION public.set_report_customer_config_updated_at();

ALTER TABLE public.report_customer_config OWNER TO fewo;
ALTER TABLE public.monthly_report_runs OWNER TO fewo;
ALTER SEQUENCE IF EXISTS public.monthly_report_runs_id_seq OWNER TO fewo;
ALTER FUNCTION public.set_report_customer_config_updated_at() OWNER TO fewo;

GRANT SELECT, INSERT, UPDATE, DELETE
    ON public.report_customer_config, public.monthly_report_runs
    TO fewo;

GRANT USAGE, SELECT, UPDATE
    ON SEQUENCE public.monthly_report_runs_id_seq
    TO fewo;

COMMIT;
