-- Migration: Add admin users and company settings
-- Date: 2025-01-XX
-- Description: Creates admin_users table and extends companies table with billing and usage settings

-- ===== Admin Users =====
CREATE TABLE IF NOT EXISTS admin_users (
    id VARCHAR(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP NULL DEFAULT NULL,
    INDEX idx_email (email),
    INDEX idx_deleted_at (deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ===== Extend Companies Table =====
-- Add billing and usage limit columns to companies table
ALTER TABLE companies 
ADD COLUMN billing_amount DECIMAL(12, 2) DEFAULT NULL COMMENT '課金金額',
ADD COLUMN usage_limit_date DATE DEFAULT NULL COMMENT '利用期限',
ADD COLUMN max_users INT DEFAULT NULL COMMENT '利用上限ユーザー数',
ADD COLUMN auth_key VARCHAR(255) DEFAULT NULL COMMENT '認証キー（ユーザー登録用）',
ADD COLUMN deleted_at TIMESTAMP NULL DEFAULT NULL COMMENT '削除日時（soft delete）';

-- Add indexes for new columns
ALTER TABLE companies
ADD INDEX idx_deleted_at (deleted_at),
ADD INDEX idx_auth_key (auth_key);

