-- +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.865', '2026-03-02 21:24:36.779730', NULL, 'Čeština', 'cs', 'cs', '__-__-____', '__-__', 0, 0, 0, '🇨🇿'),
  (4, '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_repo_accesses
-- CREATE TABLE IF NOT EXISTS b2b_customer_repo_accesses (
--   user_id BIGINT NOT NULL,
--   repo_id BIGINT NOT NULL,
--   PRIMARY KEY (user_id, repo_id),
--   CONSTRAINT fk_customer_repo_user
--     FOREIGN KEY (user_id) REFERENCES b2b_customers(id)
--     ON DELETE CASCADE,
--   CONSTRAINT fk_customer_repo_repo
--     FOREIGN KEY (repo_id) REFERENCES repository(id)
--     ON DELETE CASCADE
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


-- 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    INT UNSIGNED NOT NULL,
  flag        VARCHAR(16) NOT NULL,
  CONSTRAINT fk_countries_currency FOREIGN KEY (currency) REFERENCES ps_currency(id_currency) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

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

-- +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;
