-- +goose Up

CREATE TABLE IF NOT EXISTS b2b_language (
  id                INT AUTO_INCREMENT PRIMARY KEY,
  created_at        DATETIME(6) NOT NULL,
  updated_at        DATETIME(6) NULL,
  deleted_at        DATETIME(6) NULL,
  name              VARCHAR(128) NOT NULL,
  iso_code          VARCHAR(2) NOT NULL,
  lang_code         VARCHAR(5) NOT NULL,
  date_format       VARCHAR(32) NOT NULL,
  date_format_short VARCHAR(32) NOT NULL,
  rtl               TINYINT(1) NOT NULL DEFAULT 0,
  is_default        TINYINT(1) NOT NULL DEFAULT 0,
  active            TINYINT(1) NOT NULL DEFAULT 1,
  flag              VARCHAR(16) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE INDEX IF NOT EXISTS idx_language_deleted_at
ON b2b_language (deleted_at);

INSERT IGNORE INTO b2b_language
  (id, created_at, updated_at, deleted_at, name, iso_code, lang_code, date_format, date_format_short, rtl, is_default, active, flag)
VALUES
  (1, '2022-09-16 17:10:02.837', '2026-03-02 21:24:36.779730', NULL, 'Polski', 'pl', 'pl', '__-__-____', '__-__', 0, 0, 1, '🇵🇱'),
  (2, '2022-09-16 17:10:02.852', '2026-03-02 21:24:36.779730', NULL, 'English', 'en', 'en', '__-__-____', '__-__', 0, 1, 1, '🇬🇧'),
  (3, '2022-09-16 17:10:02.852', '2026-03-02 21:24:36.779730', NULL, 'Deutsch', 'de', 'de', '__-__-____', '__-__', 0, 0, 1, '🇩🇪');

CREATE TABLE IF NOT EXISTS b2b_components (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
CREATE UNIQUE INDEX IF NOT EXISTS uk_components_name ON b2b_components (name, id);
-- scopes
CREATE TABLE IF NOT EXISTS b2b_scopes (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
CREATE UNIQUE INDEX IF NOT EXISTS uk_scopes_name ON b2b_scopes (name);
-- translations
CREATE TABLE IF NOT EXISTS b2b_translations (
  lang_id INT NOT NULL,
  scope_id INT NOT NULL,
  component_id INT NOT NULL,
  `key` VARCHAR(255) NOT NULL,
  data TEXT NULL,
  PRIMARY KEY (lang_id, scope_id, component_id, `key`),
  CONSTRAINT fk_translations_language FOREIGN KEY (lang_id) REFERENCES b2b_language(id) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT fk_translations_scope FOREIGN KEY (scope_id) REFERENCES b2b_scopes(id) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT fk_translations_component FOREIGN KEY (component_id) REFERENCES b2b_components(id) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;



-- customers
CREATE TABLE IF NOT EXISTS b2b_customers (
  id                             BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  email                          VARCHAR(255) NOT NULL,
  password                       VARCHAR(255) NULL,
  first_name                     VARCHAR(100) NULL,
  last_name                      VARCHAR(100) NULL,
  role                           VARCHAR(20) NULL DEFAULT 'user',
  provider                       VARCHAR(20) NULL DEFAULT 'local',
  provider_id                    VARCHAR(255) NULL,
  avatar_url                      VARCHAR(500) NULL,
  is_active                       TINYINT(1) NULL DEFAULT 1,
  email_verified                  TINYINT(1) NULL DEFAULT 0,
  email_verification_token        VARCHAR(255) NULL,
  email_verification_expires      DATETIME(6) NULL,
  password_reset_token            VARCHAR(255) NULL,
  password_reset_expires          DATETIME(6) NULL,
  last_password_reset_request     DATETIME(6) NULL,
  last_login_at                   DATETIME(6) NULL,
  lang_id                         BIGINT NULL DEFAULT 2,
  country_id                      BIGINT NULL DEFAULT 2,
  created_at                      DATETIME(6) NULL,
  updated_at                      DATETIME(6) NULL,
  deleted_at                      DATETIME(6) NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE UNIQUE INDEX IF NOT EXISTS idx_customers_email
ON b2b_customers (email);

CREATE INDEX IF NOT EXISTS idx_customers_deleted_at
ON b2b_customers (deleted_at);


-- customer_carts
CREATE TABLE IF NOT EXISTS b2b_customer_carts (
  cart_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT UNSIGNED NOT NULL,
  name VARCHAR(255) NULL,
  CONSTRAINT fk_customer_carts_customers FOREIGN KEY (user_id) REFERENCES b2b_customers(id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
CREATE INDEX IF NOT EXISTS idx_customer_carts_user_id ON b2b_customer_carts (user_id);


-- carts_products
CREATE TABLE IF NOT EXISTS b2b_carts_products (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  cart_id BIGINT UNSIGNED NOT NULL,
  product_id INT UNSIGNED NOT NULL,
  product_attribute_id BIGINT NULL,
  amount INT UNSIGNED NOT NULL,
  CONSTRAINT fk_carts_products_customer_carts FOREIGN KEY (cart_id) REFERENCES b2b_customer_carts (cart_id) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_carts_products_product FOREIGN KEY (product_id) REFERENCES ps_product (id_product) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
CREATE INDEX IF NOT EXISTS idx_carts_products_cart_id ON b2b_carts_products (cart_id);


-- refresh_tokens
CREATE TABLE IF NOT EXISTS b2b_refresh_tokens (
  id INT AUTO_INCREMENT PRIMARY KEY,
  customer_id BIGINT UNSIGNED NOT NULL,
  token_hash VARCHAR(64) NOT NULL,
  expires_at DATETIME(6) NOT NULL,
  created_at DATETIME(6) NOT NULL,
  CONSTRAINT fk_refresh_tokens_customer FOREIGN KEY (customer_id) REFERENCES b2b_customers(id) ON DELETE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
CREATE UNIQUE INDEX IF NOT EXISTS uk_refresh_tokens_token_hash ON b2b_refresh_tokens (token_hash);
CREATE INDEX IF NOT EXISTS idx_refresh_tokens_customer_id ON b2b_refresh_tokens (customer_id);


-- countries
CREATE TABLE IF NOT EXISTS b2b_countries (
  id          INT AUTO_INCREMENT PRIMARY KEY,
  name        VARCHAR(128) NOT NULL,
  currency_id INT UNSIGNED NOT NULL,
  flag        VARCHAR(16) NOT NULL,
  CONSTRAINT fk_countries_currency FOREIGN KEY (currency_id) REFERENCES ps_currency(id_currency) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO b2b_countries
  (id, name, currency_id, flag)
VALUES
  (1, 'Polska', 1, '🇵🇱'),
  (2, 'England', 2, '🇬🇧'),
  (3, 'Čeština', 2, '🇨🇿'),
  (4, 'Deutschland', 2, '🇩🇪');


DELIMITER //

CREATE FUNCTION IF NOT EXISTS slugify_eu(input TEXT)
RETURNS TEXT
DETERMINISTIC
BEGIN
    DECLARE s TEXT;

    SET s = LOWER(input);

    -- spaces
    SET s = REPLACE(s,' ','_');

    -- Polish
    SET s = REPLACE(s,'ą','a');
    SET s = REPLACE(s,'ć','c');
    SET s = REPLACE(s,'ę','e');
    SET s = REPLACE(s,'ł','l');
    SET s = REPLACE(s,'ń','n');
    SET s = REPLACE(s,'ó','o');
    SET s = REPLACE(s,'ś','s');
    SET s = REPLACE(s,'ż','z');
    SET s = REPLACE(s,'ź','z');

    -- German
    SET s = REPLACE(s,'ä','a');
    SET s = REPLACE(s,'ö','o');
    SET s = REPLACE(s,'ü','u');
    SET s = REPLACE(s,'ß','ss');

    -- French
    SET s = REPLACE(s,'à','a');
    SET s = REPLACE(s,'â','a');
    SET s = REPLACE(s,'æ','ae');
    SET s = REPLACE(s,'ç','c');
    SET s = REPLACE(s,'è','e');
    SET s = REPLACE(s,'é','e');
    SET s = REPLACE(s,'ê','e');
    SET s = REPLACE(s,'ë','e');
    SET s = REPLACE(s,'î','i');
    SET s = REPLACE(s,'ï','i');
    SET s = REPLACE(s,'ô','o');
    SET s = REPLACE(s,'ù','u');
    SET s = REPLACE(s,'û','u');
    SET s = REPLACE(s,'ü','u');
    SET s = REPLACE(s,'ÿ','y');

    -- Spanish / Portuguese
    SET s = REPLACE(s,'á','a');
    SET s = REPLACE(s,'í','i');
    SET s = REPLACE(s,'ñ','n');

    -- Scandinavian
    SET s = REPLACE(s,'å','a');
    SET s = REPLACE(s,'ø','o');

    RETURN s;
END //

DELIMITER ;


DELIMITER $$

CREATE FUNCTION IF NOT EXISTS get_subcategories(startCategory INT)
RETURNS TEXT
DETERMINISTIC
BEGIN
    DECLARE result TEXT;

    -- Use GROUP_CONCAT to aggregate all IDs into a single string
    WITH RECURSIVE subcategories AS (
        SELECT c.id_category
        FROM ps_category c
        WHERE c.id_category = startCategory

        UNION ALL

        SELECT c.id_category
        FROM ps_category c
        INNER JOIN subcategories sc ON c.id_parent = sc.id_category
    )
    SELECT GROUP_CONCAT(id_category) INTO result
    FROM subcategories;

    RETURN result;
END$$

DELIMITER ;

-- +goose Down

DROP TABLE IF EXISTS b2b_countries;
DROP TABLE IF EXISTS b2b_language;
DROP TABLE IF EXISTS b2b_components;
DROP TABLE IF EXISTS b2b_scopes;
DROP TABLE IF EXISTS b2b_translations;
DROP TABLE IF EXISTS b2b_customers;
DROP TABLE IF EXISTS b2b_refresh_tokens;
