CREATE TABLE `identity_accounts` (
    `id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
    `email` varchar(255) NOT NULL,
    `password_hash` varchar(512) NOT NULL,
    `name` varchar(255) NOT NULL,
    `avatar_url` varchar(255) DEFAULT NULL,
    `language_code` varchar(2) NOT NULL,
    `status` varchar(255) NOT NULL,
    `created_at` datetime NOT NULL,
    `updated_at` datetime NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uq_identity_accounts_email` (`email`),
    KEY `idx_identity_accounts_status` (`status`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;

CREATE TABLE `identity_company_memberships` (
    `id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
    `account_id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
    `company_id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
    `status` varchar(255) NOT NULL,
    `created_at` datetime NOT NULL,
    `updated_at` datetime NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uq_identity_company_memberships_account_company` (`account_id`, `company_id`),
    KEY `idx_identity_company_memberships_account_id` (`account_id`),
    KEY `idx_identity_company_memberships_company_id` (`company_id`),
    KEY `idx_identity_company_memberships_status` (`status`),
    CONSTRAINT `identity_company_memberships_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `identity_accounts` (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;

CREATE TABLE `identity_refresh_tokens` (
    `id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
    `account_id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
    `token_hash` varchar(512) NOT NULL,
    `expires_at` datetime NOT NULL,
    `revoked_at` datetime DEFAULT NULL,
    `replaced_by_token_id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
    `created_at` datetime NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uq_identity_refresh_tokens_hash` (`token_hash`),
    KEY `idx_identity_refresh_tokens_account_id` (`account_id`),
    KEY `idx_identity_refresh_tokens_expires_at` (`expires_at`),
    KEY `idx_identity_refresh_tokens_revoked_at` (`revoked_at`),
    KEY `replaced_by_token_id` (`replaced_by_token_id`),
    CONSTRAINT `identity_refresh_tokens_ibfk_285` FOREIGN KEY (`account_id`) REFERENCES `identity_accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `identity_refresh_tokens_ibfk_286` FOREIGN KEY (`replaced_by_token_id`) REFERENCES `identity_refresh_tokens` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;