CREATE TABLE tracking_links
(
    id              uuid                                   NOT NULL,
    url             character varying,
    tracking_number character varying                      NOT NULL,
    fulfillment_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,
    idempotency_key character varying
);

CREATE TABLE custom_shipping_options
(
    id                 uuid                                   NOT NULL,
    price              integer                                NOT NULL,
    shipping_option_id uuid                                   NOT NULL,
    cart_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 shipping_methods
(
    id                 uuid    NOT NULL,
    shipping_option_id uuid    NOT NULL,
    order_id           uuid,
    cart_id            uuid,
    swap_id            uuid,
    return_id          uuid,
    price              integer NOT NULL,
    data               jsonb   NOT NULL,
    claim_order_id     uuid,
    CONSTRAINT "CHK_64c6812fe7815be30d688df513" CHECK ((price >= 0)),
    CONSTRAINT "CHK_a7020b08665bbd64d84a6641cf" CHECK (((claim_order_id IS NOT NULL) OR (order_id IS NOT NULL) OR
                                                        (cart_id IS NOT NULL) OR (swap_id IS NOT NULL) OR
                                                        (return_id IS NOT NULL)))
);

CREATE TABLE shipping_method_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,
    shipping_method_id uuid                                   NOT NULL
);

CREATE TABLE shipping_options
(
    id          uuid                                   NOT NULL,
    name        character varying                      NOT NULL,
    region_id   uuid                                   NOT NULL,
    profile_id  uuid                                   NOT NULL,
    provider_id uuid                                   NOT NULL,
    price_type  shipping_option_price_types            NOT NULL,
    amount      integer,
    is_return   boolean                  DEFAULT false 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,
    deleted_at  timestamp with time zone,
    metadata    jsonb,
    admin_only  boolean                  DEFAULT false NOT NULL,
    CONSTRAINT "CHK_7a367f5901ae0a5b0df75aee38" CHECK ((amount >= 0))
);

CREATE TABLE shipping_option_requirements
(
    id                 uuid                              NOT NULL,
    shipping_option_id uuid                              NOT NULL,
    type               shipping_option_requirement_types NOT NULL,
    amount             integer                           NOT NULL,
    deleted_at         timestamp with time zone
);

CREATE TABLE shipping_profiles
(
    id         uuid                                   NOT NULL,
    name       character varying                      NOT NULL,
    type       shipping_profile_types                 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 shipping_tax_rates
(
    shipping_option_id uuid                                   NOT NULL,
    rate_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 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 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
);
