CREATE TABLE IF NOT EXISTS "data_assets"
(
    id                        UUID PRIMARY KEY     DEFAULT gen_random_uuid(),
    asset_name                TEXT        NOT NULL,
    asset_type                TEXT        NOT NULL,
    domain                    TEXT        NOT NULL,
    application_scene         TEXT,
    data_description          TEXT        NOT NULL,
    data_scale                TEXT        NOT NULL,
    collection_method         TEXT        NOT NULL,
    labeling_status           TEXT,
    update_frequency          TEXT,
    privacy_level             TEXT        NOT NULL,
    permission_mode           TEXT        NOT NULL,
    supports_validation       BOOLEAN     NOT NULL,
    seller_expected_price_min NUMERIC(20, 2),
    seller_expected_price_max NUMERIC(20, 2),
    quality_level             TEXT,
    scarcity_level            TEXT,
    base_value_score          NUMERIC(20, 2),
    base_price_min            NUMERIC(20, 2),
    base_price_max            NUMERIC(20, 2),
    asset_status              TEXT        NOT NULL,
    created_at                TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at                TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS "buyer_requests"
(
    id                  UUID PRIMARY KEY     DEFAULT gen_random_uuid(),
    asset_id            UUID        NOT NULL REFERENCES data_assets (id),
    task_type           TEXT        NOT NULL,
    model_type          TEXT        NOT NULL,
    buyer_budget_min    NUMERIC(20, 2),
    buyer_budget_max    NUMERIC(20, 2),
    privacy_requirement TEXT,
    usage_purpose       TEXT,
    request_note        TEXT,
    request_status      TEXT        NOT NULL,
    created_at          TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at          TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS "pricing_results"
(
    id                      UUID PRIMARY KEY     DEFAULT gen_random_uuid(),
    asset_id                UUID        NOT NULL REFERENCES data_assets (id),
    request_id              UUID REFERENCES buyer_requests (id),
    scenario_value_score    NUMERIC(20, 2),
    scenario_price_min      NUMERIC(20, 2),
    scenario_price_max      NUMERIC(20, 2),
    suggested_price         NUMERIC(20, 2),
    success_probability     NUMERIC(5, 4),
    pricing_reason_1        TEXT,
    pricing_reason_2        TEXT,
    pricing_reason_3        TEXT,
    verification_suggestion TEXT,
    pricing_status          TEXT        NOT NULL,
    created_at              TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at              TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS "validations"
(
    id                      UUID PRIMARY KEY     DEFAULT gen_random_uuid(),
    asset_id                UUID        NOT NULL REFERENCES data_assets (id),
    request_id              UUID REFERENCES buyer_requests (id),
    validation_type         TEXT,
    validation_requested    BOOLEAN     NOT NULL,
    validation_status       TEXT        NOT NULL,
    validation_signal       TEXT,
    validation_score        NUMERIC(5, 4),
    risk_warning            TEXT,
    continue_recommendation TEXT,
    validation_created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
    validation_finished_at  TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS "orders"
(
    id               UUID PRIMARY KEY        DEFAULT gen_random_uuid(),
    asset_id         UUID           NOT NULL REFERENCES data_assets (id),
    request_id       UUID REFERENCES buyer_requests (id),
    pricing_id       UUID REFERENCES pricing_results (id),
    validation_id    UUID REFERENCES validations (id),
    asset_name       TEXT           NOT NULL,
    current_price    NUMERIC(20, 2) NOT NULL,
    negotiation_min  NUMERIC(20, 2),
    negotiation_max  NUMERIC(20, 2),
    validation_used  BOOLEAN        NOT NULL,
    delivery_mode    TEXT           NOT NULL,
    order_status     TEXT           NOT NULL,
    order_created_at TIMESTAMPTZ    NOT NULL DEFAULT now(),
    order_updated_at TIMESTAMPTZ    NOT NULL DEFAULT now()
);
