CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA
    COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';

CREATE TYPE payment_collection_statuses AS ENUM (
    'not_paid',
    'awaiting',
    'authorized',
    'partially_authorized',
    'canceled'
    );

CREATE TYPE payment_collection_types AS ENUM (
    'order_edit'
    );

CREATE TYPE cart_types AS ENUM (
    'default',
    'swap',
    'draft_order',
    'payment_link',
    'claim'
    );

CREATE TYPE claim_item_reasons AS ENUM (
    'missing_item',
    'wrong_item',
    'production_failure',
    'other'
    );

CREATE TYPE claim_order_fulfillment_statuses AS ENUM (
    'not_fulfilled',
    'partially_fulfilled',
    'fulfilled',
    'partially_shipped',
    'shipped',
    'partially_returned',
    'returned',
    'canceled',
    'requires_action'
    );

CREATE TYPE claim_order_payment_statuses AS ENUM (
    'na',
    'not_refunded',
    'refunded'
    );

CREATE TYPE claim_order_types AS ENUM (
    'refund',
    'replace'
    );

CREATE TYPE discount_condition_operators AS ENUM (
    'in',
    'not_in'
    );

CREATE TYPE discount_condition_types AS ENUM (
    'products',
    'product_types',
    'product_collections',
    'product_tags',
    'customer_groups'
    );

CREATE TYPE discount_rule_allocations AS ENUM (
    'total',
    'item'
    );

CREATE TYPE discount_rule_types AS ENUM (
    'fixed',
    'percentage',
    'free_shipping'
    );

CREATE TYPE draft_order_statuses AS ENUM (
    'open',
    'completed'
    );

CREATE TYPE invite_roles AS ENUM (
    'admin',
    'member',
    'developer'
    );

CREATE TYPE order_fulfillment_statuses AS ENUM (
    'not_fulfilled',
    'partially_fulfilled',
    'fulfilled',
    'partially_shipped',
    'shipped',
    'partially_returned',
    'returned',
    'canceled',
    'requires_action'
    );

CREATE TYPE order_item_change_types AS ENUM (
    'item_add',
    'item_remove',
    'item_update'
    );

CREATE TYPE order_payment_statuses AS ENUM (
    'not_paid',
    'awaiting',
    'captured',
    'partially_refunded',
    'refunded',
    'canceled',
    'requires_action'
    );

CREATE TYPE order_statuses AS ENUM (
    'pending',
    'completed',
    'archived',
    'canceled',
    'requires_action'
    );

CREATE TYPE payment_session_statuses AS ENUM (
    'authorized',
    'pending',
    'requires_more',
    'error',
    'canceled'
    );

CREATE TYPE price_list_statuses AS ENUM (
    'active',
    'draft'
    );
CREATE TYPE price_list_types AS ENUM (
    'sale',
    'override'
    );

CREATE TYPE product_statuses AS ENUM (
    'draft',
    'proposed',
    'published',
    'rejected'
    );

CREATE TYPE refund_reasons AS ENUM (
    'discount',
    'return',
    'swap',
    'claim',
    'other'
    );

CREATE TYPE return_statuses AS ENUM (
    'requested',
    'received',
    'requires_action',
    'canceled'
    );

CREATE TYPE shipping_option_price_types AS ENUM (
    'flat_rate',
    'calculated'
    );

CREATE TYPE shipping_option_requirement_types AS ENUM (
    'min_subtotal',
    'max_subtotal'
    );

CREATE TYPE shipping_profile_types AS ENUM (
    'default',
    'gift_card',
    'custom'
    );

CREATE TYPE swap_fulfillment_statuses AS ENUM (
    'not_fulfilled',
    'fulfilled',
    'shipped',
    'partially_shipped',
    'canceled',
    'requires_action'
    );

CREATE TYPE swap_payment_statuses AS ENUM (
    'not_paid',
    'awaiting',
    'captured',
    'confirmed',
    'canceled',
    'difference_refunded',
    'partially_refunded',
    'refunded',
    'requires_action'
    );

CREATE TYPE user_roles AS ENUM (
    'admin',
    'member',
    'developer'
    );

CREATE TABLE addresses
(
    id           uuid                                   NOT NULL,
    customer_id  uuid,
    company      character varying,
    first_name   character varying,
    last_name    character varying,
    address_1    character varying,
    address_2    character varying,
    city         character varying,
    country_code character varying,
    province     character varying,
    postal_code  character varying,
    phone        character varying,
    created_at   timestamp with time zone DEFAULT now() NOT NULL,
    updated_at   timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at   timestamp with time zone,
    metadata     jsonb
);

CREATE TABLE analytics_configs
(
    id         uuid                                   NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at timestamp with time zone,
    user_id    uuid                                   NOT NULL,
    opt_out    boolean                  DEFAULT false NOT NULL,
    anonymize  boolean                  DEFAULT false NOT NULL
);

CREATE TABLE batch_jobs
(
    id               uuid                                   NOT NULL,
    type             text                                   NOT NULL,
    created_by       character varying,
    context          jsonb,
    result           jsonb,
    dry_run          boolean                  DEFAULT false NOT NULL,
    created_at       timestamp with time zone DEFAULT now() NOT NULL,
    pre_processed_at timestamp with time zone,
    confirmed_at     timestamp with time zone,
    processing_at    timestamp with time zone,
    completed_at     timestamp with time zone,
    failed_at        timestamp with time zone,
    canceled_at      timestamp with time zone,
    updated_at       timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at       timestamp with time zone
);

CREATE TABLE carts
(
    id                    uuid                                                   NOT NULL,
    email                 character varying,
    billing_address_id    uuid,
    shipping_address_id   uuid,
    region_id             uuid                                                   NOT NULL,
    customer_id           uuid,
    payment_id            uuid,
    type                  cart_types               DEFAULT 'default'::cart_types NOT NULL,
    completed_at          timestamp with time zone,
    created_at            timestamp with time zone DEFAULT now()                 NOT NULL,
    updated_at            timestamp with time zone DEFAULT now()                 NOT NULL,
    deleted_at            timestamp with time zone,
    metadata              jsonb,
    idempotency_key       character varying,
    context               jsonb,
    payment_authorized_at timestamp with time zone,
    sales_channel_id      uuid
);

CREATE TABLE cart_discounts
(
    cart_id     uuid NOT NULL,
    discount_id uuid NOT NULL
);

CREATE TABLE cart_gift_cards
(
    cart_id      uuid NOT NULL,
    gift_card_id uuid NOT NULL
);

CREATE TABLE claim_images
(
    id            uuid                                   NOT NULL,
    claim_item_id uuid                                   NOT NULL,
    url           character varying                      NOT NULL,
    created_at    timestamp with time zone DEFAULT now() NOT NULL,
    updated_at    timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at    timestamp with time zone,
    metadata      jsonb
);

CREATE TABLE claim_items
(
    id             uuid                                   NOT NULL,
    claim_order_id uuid                                   NOT NULL,
    item_id        uuid                                   NOT NULL,
    variant_id     uuid                                   NOT NULL,
    reason         claim_item_reasons                     NOT NULL,
    note           character varying,
    quantity       integer                                NOT NULL,
    created_at     timestamp with time zone DEFAULT now() NOT NULL,
    updated_at     timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at     timestamp with time zone,
    metadata       jsonb
);

CREATE TABLE claim_item_tags
(
    item_id uuid NOT NULL,
    tag_id  uuid NOT NULL
);

CREATE TABLE claim_orders
(
    id                  uuid                                                                                       NOT NULL,
    payment_status      claim_order_payment_statuses     DEFAULT 'na'::claim_order_payment_statuses                NOT NULL,
    fulfillment_status  claim_order_fulfillment_statuses DEFAULT 'not_fulfilled'::claim_order_fulfillment_statuses NOT NULL,
    type                claim_order_types                                                                          NOT NULL,
    order_id            uuid                                                                                       NOT NULL,
    shipping_address_id uuid,
    refund_amount       integer,
    canceled_at         timestamp with time zone,
    created_at          timestamp with time zone         DEFAULT now()                                             NOT NULL,
    updated_at          timestamp with time zone         DEFAULT now()                                             NOT NULL,
    deleted_at          timestamp with time zone,
    metadata            jsonb,
    idempotency_key     character varying,
    no_notification     boolean
);

CREATE TABLE claim_tags
(
    id         uuid                                   NOT NULL,
    value      character varying                      NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at timestamp with time zone,
    metadata   jsonb
);


CREATE TABLE regions
(
    id                 uuid                                   NOT NULL,
    name               character varying                      NOT NULL,
    currency_code      character varying                      NOT NULL,
    tax_rate           real                                   NOT NULL,
    tax_code           character varying,
    created_at         timestamp with time zone DEFAULT now() NOT NULL,
    updated_at         timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at         timestamp with time zone,
    metadata           jsonb,
    gift_cards_taxable boolean                  DEFAULT true  NOT NULL,
    automatic_taxes    boolean                  DEFAULT true  NOT NULL,
    tax_provider_id    uuid
);

CREATE TABLE countries
(
    id           sequence          NOT NULL,
    iso_2        character varying NOT NULL,
    iso_3        character varying NOT NULL,
    num_code     integer           NOT NULL,
    name         character varying NOT NULL,
    display_name character varying NOT NULL,
    region_id    uuid
);

CREATE TABLE currencies
(
    code          character varying NOT NULL,
    symbol        character varying NOT NULL,
    symbol_native character varying NOT NULL,
    name          character varying NOT NULL
);

CREATE TABLE discounts
(
    id                 uuid                                               NOT NULL,
    code               character varying                                  NOT NULL,
    is_dynamic         boolean                                            NOT NULL,
    rule_id            uuid,
    is_disabled        boolean                                            NOT NULL,
    parent_discount_id uuid,
    starts_at          timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
    ends_at            timestamp with time zone,
    created_at         timestamp with time zone DEFAULT now()             NOT NULL,
    updated_at         timestamp with time zone DEFAULT now()             NOT NULL,
    deleted_at         timestamp with time zone,
    metadata           jsonb,
    usage_limit        integer,
    usage_count        integer                  DEFAULT 0                 NOT NULL,
    valid_duration     character varying
);

CREATE TABLE discount_conditions
(
    id               uuid                                   NOT NULL,
    type             discount_condition_types               NOT NULL,
    operator         discount_condition_operators           NOT NULL,
    discount_rule_id uuid                                   NOT NULL,
    created_at       timestamp with time zone DEFAULT now() NOT NULL,
    updated_at       timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at       timestamp with time zone,
    metadata         jsonb
);

CREATE TABLE discount_condition_customer_groups
(
    customer_group_id uuid                                   NOT NULL,
    condition_id      uuid                                   NOT NULL,
    created_at        timestamp with time zone DEFAULT now() NOT NULL,
    updated_at        timestamp with time zone DEFAULT now() NOT NULL,
    metadata          jsonb
);

CREATE TABLE discount_condition_products
(
    product_id   uuid                                   NOT NULL,
    condition_id uuid                                   NOT NULL,
    created_at   timestamp with time zone DEFAULT now() NOT NULL,
    updated_at   timestamp with time zone DEFAULT now() NOT NULL,
    metadata     jsonb
);

CREATE TABLE discount_condition_product_collections
(
    product_collection_id uuid                                   NOT NULL,
    condition_id          uuid                                   NOT NULL,
    created_at            timestamp with time zone DEFAULT now() NOT NULL,
    updated_at            timestamp with time zone DEFAULT now() NOT NULL,
    metadata              jsonb
);

CREATE TABLE discount_condition_product_tags
(
    product_tag_id uuid                                   NOT NULL,
    condition_id   uuid                                   NOT NULL,
    created_at     timestamp with time zone DEFAULT now() NOT NULL,
    updated_at     timestamp with time zone DEFAULT now() NOT NULL,
    metadata       jsonb
);

CREATE TABLE discount_condition_product_types
(
    product_type_id uuid                                   NOT NULL,
    condition_id    uuid                                   NOT NULL,
    created_at      timestamp with time zone DEFAULT now() NOT NULL,
    updated_at      timestamp with time zone DEFAULT now() NOT NULL,
    metadata        jsonb
);

CREATE TABLE discount_regions
(
    discount_id uuid NOT NULL,
    region_id   uuid NOT NULL
);

CREATE TABLE discount_rules
(
    id          uuid                                   NOT NULL,
    description character varying,
    type        discount_rule_types                    NOT NULL,
    value       integer                                NOT NULL,
    allocation  discount_rule_allocations,
    created_at  timestamp with time zone DEFAULT now() NOT NULL,
    updated_at  timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at  timestamp with time zone,
    metadata    jsonb
);

CREATE TABLE discount_rule_products
(
    discount_rule_id uuid NOT NULL,
    product_id       uuid NOT NULL
);

CREATE TABLE orders
(
    id                  uuid                                                                           NOT NULL,
    status              order_statuses             DEFAULT 'pending'::order_statuses                   NOT NULL,
    fulfillment_status  order_fulfillment_statuses DEFAULT 'not_fulfilled'::order_fulfillment_statuses NOT NULL,
    payment_status      order_payment_statuses     DEFAULT 'not_paid'::order_payment_statuses          NOT NULL,
    display_id          integer                                                                        NOT NULL,
    cart_id             uuid,
    customer_id         uuid                                                                           NOT NULL,
    email               character varying                                                              NOT NULL,
    billing_address_id  uuid,
    shipping_address_id uuid,
    region_id           uuid                                                                           NOT NULL,
    currency_code       character varying                                                              NOT NULL,
    tax_rate            real,
    canceled_at         timestamp with time zone,
    created_at          timestamp with time zone   DEFAULT now()                                       NOT NULL,
    updated_at          timestamp with time zone   DEFAULT now()                                       NOT NULL,
    metadata            jsonb,
    idempotency_key     character varying,
    draft_order_id      uuid,
    no_notification     boolean,
    external_id         uuid,
    sales_channel_id    uuid
);

CREATE TABLE order_discounts
(
    order_id    uuid NOT NULL,
    discount_id uuid NOT NULL
);

CREATE TABLE order_edits
(
    id                    uuid                                   NOT NULL,
    created_at            timestamp with time zone DEFAULT now() NOT NULL,
    updated_at            timestamp with time zone DEFAULT now() NOT NULL,
    order_id              uuid                                   NOT NULL,
    internal_note         character varying,
    created_by            character varying                      NOT NULL,
    requested_by          character varying,
    requested_at          timestamp with time zone,
    confirmed_by          character varying,
    confirmed_at          timestamp with time zone,
    declined_by           character varying,
    declined_reason       character varying,
    declined_at           timestamp with time zone,
    canceled_by           character varying,
    canceled_at           timestamp with time zone,
    payment_collection_id uuid
);

CREATE TABLE order_gift_cards
(
    order_id     uuid NOT NULL,
    gift_card_id uuid NOT NULL
);

CREATE TABLE order_item_changes
(
    id                    uuid                                   NOT NULL,
    created_at            timestamp with time zone DEFAULT now() NOT NULL,
    updated_at            timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at            timestamp with time zone,
    type                  order_item_change_types                NOT NULL,
    order_edit_id         uuid                                   NOT NULL,
    original_line_item_id uuid,
    line_item_id          uuid
);

CREATE TABLE payments
(
    id              uuid                                   NOT NULL,
    swap_id         uuid,
    cart_id         uuid,
    order_id        uuid,
    amount          integer                                NOT NULL,
    currency_code   character varying                      NOT NULL,
    amount_refunded integer                  DEFAULT 0     NOT NULL,
    provider_id     uuid                                   NOT NULL,
    data            jsonb                                  NOT NULL,
    captured_at     timestamp with time zone,
    canceled_at     timestamp with time zone,
    created_at      timestamp with time zone DEFAULT now() NOT NULL,
    updated_at      timestamp with time zone DEFAULT now() NOT NULL,
    metadata        jsonb,
    idempotency_key character varying
);

CREATE TABLE payment_collections
(
    id                uuid                                   NOT NULL,
    created_at        timestamp with time zone DEFAULT now() NOT NULL,
    updated_at        timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at        timestamp with time zone,
    type              payment_collection_types               NOT NULL,
    status            payment_collection_statuses            NOT NULL,
    description       text,
    amount            integer                                NOT NULL,
    authorized_amount integer,
    region_id         uuid                                   NOT NULL,
    currency_code     character varying                      NOT NULL,
    metadata          jsonb,
    created_by        character varying                      NOT NULL
);

CREATE TABLE payment_collection_payments
(
    payment_collection_id uuid NOT NULL,
    payment_id            uuid NOT NULL
);

CREATE TABLE payment_collection_sessions
(
    payment_collection_id uuid NOT NULL,
    payment_session_id    uuid NOT NULL
);

CREATE TABLE payment_providers
(
    id           uuid                 NOT NULL,
    is_installed boolean DEFAULT true NOT NULL
);

CREATE TABLE payment_sessions
(
    id                    uuid                                   NOT NULL,
    cart_id               uuid,
    provider_id           uuid                                   NOT NULL,
    is_selected           boolean,
    status                payment_session_statuses               NOT NULL,
    data                  jsonb                                  NOT NULL,
    created_at            timestamp with time zone DEFAULT now() NOT NULL,
    updated_at            timestamp with time zone DEFAULT now() NOT NULL,
    idempotency_key       character varying,
    payment_authorized_at timestamp with time zone,
    amount                integer,
    is_initiated          boolean                  DEFAULT false NOT NULL
);

CREATE TABLE gift_cards
(
    id          uuid                                   NOT NULL,
    code        character varying                      NOT NULL,
    value       integer                                NOT NULL,
    balance     integer                                NOT NULL,
    region_id   uuid                                   NOT NULL,
    order_id    uuid,
    is_disabled boolean                  DEFAULT false NOT NULL,
    ends_at     timestamp with time zone,
    created_at  timestamp with time zone DEFAULT now() NOT NULL,
    updated_at  timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at  timestamp with time zone,
    metadata    jsonb,
    tax_rate    real
);

CREATE TABLE gift_card_transactions
(
    id           uuid                                   NOT NULL,
    gift_card_id uuid                                   NOT NULL,
    order_id     uuid                                   NOT NULL,
    amount       integer                                NOT NULL,
    created_at   timestamp with time zone DEFAULT now() NOT NULL,
    is_taxable   boolean,
    tax_rate     real
);

CREATE TABLE line_items
(
    id                 uuid                                   NOT NULL,
    cart_id            uuid,
    order_id           uuid,
    swap_id            uuid,
    title              character varying                      NOT NULL,
    description        character varying,
    thumbnail          character varying,
    is_giftcard        boolean                  DEFAULT false NOT NULL,
    should_merge       boolean                  DEFAULT true  NOT NULL,
    allow_discounts    boolean                  DEFAULT true  NOT NULL,
    has_shipping       boolean,
    unit_price         integer                                NOT NULL,
    variant_id         uuid,
    quantity           integer                                NOT NULL,
    fulfilled_quantity integer,
    returned_quantity  integer,
    shipped_quantity   integer,
    created_at         timestamp with time zone DEFAULT now() NOT NULL,
    updated_at         timestamp with time zone DEFAULT now() NOT NULL,
    metadata           jsonb,
    claim_order_id     uuid,
    is_return          boolean                  DEFAULT false NOT NULL,
    original_item_id   uuid,
    order_edit_id      uuid,
    CONSTRAINT "CHK_0cd85e15610d11b553d5e8fda6" CHECK ((shipped_quantity <= fulfilled_quantity)),
    CONSTRAINT "CHK_64eef00a5064887634f1680866" CHECK ((quantity > 0)),
    CONSTRAINT "CHK_91f40396d847f6ecfd9f752bf8" CHECK ((returned_quantity <= quantity)),
    CONSTRAINT "CHK_c61716c68f5ad5de2834c827d3" CHECK ((fulfilled_quantity <= quantity))
);

CREATE TABLE line_item_adjustments
(
    id          uuid              NOT NULL,
    item_id     uuid              NOT NULL,
    description character varying NOT NULL,
    discount_id uuid,
    amount      numeric           NOT NULL,
    metadata    jsonb
);

CREATE TABLE line_item_tax_lines
(
    id         uuid                                   NOT NULL,
    rate       real                                   NOT NULL,
    name       character varying                      NOT NULL,
    code       character varying,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    metadata   jsonb,
    item_id    uuid                                   NOT NULL
);

---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################
---- ###########################################################

CREATE TABLE draft_orders
(
    id                    uuid                                                          NOT NULL,
    status                draft_order_statuses     DEFAULT 'open'::draft_order_statuses NOT NULL,
    display_id            sequence                                                      NOT NULL,
    cart_id               uuid,
    order_id              uuid,
    canceled_at           timestamp with time zone,
    created_at            timestamp with time zone DEFAULT now()                        NOT NULL,
    updated_at            timestamp with time zone DEFAULT now()                        NOT NULL,
    completed_at          timestamp with time zone,
    metadata              jsonb,
    idempotency_key       character varying,
    no_notification_order boolean
);

CREATE TABLE fulfillments
(
    id               uuid                                         NOT NULL,
    swap_id          uuid,
    order_id         uuid,
    tracking_numbers jsonb                    DEFAULT '[]'::jsonb NOT NULL,
    data             jsonb                                        NOT NULL,
    shipped_at       timestamp with time zone,
    canceled_at      timestamp with time zone,
    created_at       timestamp with time zone DEFAULT now()       NOT NULL,
    updated_at       timestamp with time zone DEFAULT now()       NOT NULL,
    metadata         jsonb,
    idempotency_key  character varying,
    provider_id      uuid,
    claim_order_id   uuid,
    no_notification  boolean,
    location_id      uuid
);

CREATE TABLE fulfillment_items
(
    fulfillment_id uuid    NOT NULL,
    item_id        uuid    NOT NULL,
    quantity       integer NOT NULL
);

CREATE TABLE fulfillment_providers
(
    id           uuid                 NOT NULL,
    is_installed boolean DEFAULT true NOT NULL
);

CREATE TABLE images
(
    id         uuid                                   NOT NULL,
    url        character varying                      NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at timestamp with time zone,
    metadata   jsonb
);

CREATE TABLE invites
(
    id         uuid                                   NOT NULL,
    user_email character varying                      NOT NULL,
    role       invite_roles             DEFAULT 'member'::invite_roles,
    accepted   boolean                  DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at timestamp with time zone,
    metadata   jsonb,
    token      character varying                      NOT NULL,
    expires_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE notes
(
    id            uuid                                   NOT NULL,
    value         character varying                      NOT NULL,
    resource_type character varying                      NOT NULL,
    resource_id   uuid                                   NOT NULL,
    author_id     uuid,
    created_at    timestamp with time zone DEFAULT now() NOT NULL,
    updated_at    timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at    timestamp with time zone,
    metadata      jsonb
);

CREATE TABLE oauth
(
    id               uuid              NOT NULL,
    display_name     character varying NOT NULL,
    application_name character varying NOT NULL,
    install_url      character varying,
    uninstall_url    character varying,
    data             jsonb
);

CREATE TABLE publishable_api_keys
(
    id         uuid                                   NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    created_by character varying,
    revoked_by character varying,
    revoked_at timestamp with time zone,
    title      character varying                      NOT NULL
);

CREATE TABLE publishable_api_key_sales_channels
(
    sales_channel_id   uuid NOT NULL,
    publishable_key_id uuid NOT NULL
);

CREATE TABLE refunds
(
    id              uuid                                   NOT NULL,
    order_id        uuid,
    amount          integer                                NOT NULL,
    note            character varying,
    reason          refund_reasons                         NOT NULL,
    created_at      timestamp with time zone DEFAULT now() NOT NULL,
    updated_at      timestamp with time zone DEFAULT now() NOT NULL,
    metadata        jsonb,
    idempotency_key character varying,
    payment_id      uuid
);

CREATE TABLE region_fulfillment_providers
(
    region_id   uuid NOT NULL,
    provider_id uuid NOT NULL
);

CREATE TABLE region_payment_providers
(
    region_id   uuid NOT NULL,
    provider_id uuid NOT NULL
);

CREATE TABLE returns
(
    id              uuid                                                          NOT NULL,
    status          return_statuses          DEFAULT 'requested'::return_statuses NOT NULL,
    swap_id         uuid,
    order_id        uuid,
    shipping_data   jsonb,
    refund_amount   integer                                                       NOT NULL,
    received_at     timestamp with time zone,
    created_at      timestamp with time zone DEFAULT now()                        NOT NULL,
    updated_at      timestamp with time zone DEFAULT now()                        NOT NULL,
    metadata        jsonb,
    idempotency_key character varying,
    claim_order_id  uuid,
    no_notification boolean,
    location_id     uuid
);

CREATE TABLE return_items
(
    return_id          uuid                 NOT NULL,
    item_id            uuid                 NOT NULL,
    quantity           integer              NOT NULL,
    is_requested       boolean DEFAULT true NOT NULL,
    requested_quantity integer,
    received_quantity  integer,
    metadata           jsonb,
    reason_id          uuid,
    note               character varying
);

CREATE TABLE return_reasons
(
    id                      uuid                                   NOT NULL,
    value                   character varying                      NOT NULL,
    label                   character varying                      NOT NULL,
    description             character varying,
    created_at              timestamp with time zone DEFAULT now() NOT NULL,
    updated_at              timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at              timestamp with time zone,
    metadata                jsonb,
    parent_return_reason_id uuid
);

CREATE TABLE sales_channels
(
    id          uuid                                   NOT NULL,
    created_at  timestamp with time zone DEFAULT now() NOT NULL,
    updated_at  timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at  timestamp with time zone,
    name        character varying                      NOT NULL,
    description character varying,
    is_disabled boolean                  DEFAULT false NOT NULL,
    metadata    jsonb
);

CREATE TABLE sales_channel_locations
(
    id               uuid                                   NOT NULL,
    created_at       timestamp with time zone DEFAULT now() NOT NULL,
    updated_at       timestamp with time zone DEFAULT now() NOT NULL,
    sales_channel_id text                                   NOT NULL,
    location_id      text                                   NOT NULL,
    deleted_at       timestamp with time zone
);

CREATE TABLE staged_jobs
(
    id         uuid                      NOT NULL,
    event_name character varying         NOT NULL,
    data       jsonb                     NOT NULL,
    options    jsonb DEFAULT '{}'::jsonb NOT NULL
);

CREATE TABLE stores
(
    id                       uuid                                                         NOT NULL,
    name                     character varying        DEFAULT 'Bazaar'::character varying NOT NULL,
    default_currency_code    character varying        DEFAULT 'pln'::character varying    NOT NULL,
    swap_link_template       character varying,
    created_at               timestamp with time zone DEFAULT now()                       NOT NULL,
    updated_at               timestamp with time zone DEFAULT now()                       NOT NULL,
    metadata                 jsonb,
    payment_link_template    character varying,
    invite_link_template     character varying,
    default_sales_channel_id uuid,
    default_location_id      uuid
);

CREATE TABLE store_currencies
(
    store_id      uuid              NOT NULL,
    currency_code character varying NOT NULL
);

CREATE TABLE swaps
(
    id                  uuid                                   NOT NULL,
    fulfillment_status  swap_fulfillment_statuses              NOT NULL,
    payment_status      swap_payment_statuses                  NOT NULL,
    order_id            uuid                                   NOT NULL,
    difference_due      integer,
    shipping_address_id uuid,
    cart_id             uuid,
    confirmed_at        timestamp with time zone,
    created_at          timestamp with time zone DEFAULT now() NOT NULL,
    updated_at          timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at          timestamp with time zone,
    metadata            jsonb,
    idempotency_key     character varying,
    no_notification     boolean,
    canceled_at         timestamp with time zone,
    allow_backorder     boolean                  DEFAULT false NOT NULL
);

CREATE TABLE tax_providers
(
    id           uuid                 NOT NULL,
    is_installed boolean DEFAULT true NOT NULL
);

CREATE TABLE tax_rates
(
    id         uuid                                   NOT NULL,
    rate       real,
    code       character varying,
    name       character varying                      NOT NULL,
    region_id  uuid                                   NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    metadata   jsonb
);

CREATE TABLE users
(
    id            uuid                                   NOT NULL,
    email         character varying                      NOT NULL,
    first_name    character varying,
    last_name     character varying,
    password_hash character varying,
    api_token     character varying,
    created_at    timestamp with time zone DEFAULT now() NOT NULL,
    updated_at    timestamp with time zone DEFAULT now() NOT NULL,
    deleted_at    timestamp with time zone,
    metadata      jsonb,
    role          user_roles               DEFAULT 'member'::user_roles
);
