-- CreateTable
CREATE TABLE `tenants` (
    `id` VARCHAR(36) NOT NULL,
    `name` VARCHAR(100) NOT NULL,
    `slug` VARCHAR(50) NOT NULL,
    `email` VARCHAR(255) NOT NULL,
    `password_hash` VARCHAR(255) NOT NULL,
    `api_key` VARCHAR(100) NOT NULL,
    `api_secret_hash` VARCHAR(255) NOT NULL,
    `webhook_url` VARCHAR(500) NULL,
    `webhook_secret` VARCHAR(100) NULL,
    `ig_account_id` VARCHAR(100) NULL,
    `ig_access_token` TEXT NULL,
    `ig_page_id` VARCHAR(100) NULL,
    `ig_token_expires_at` DATETIME(3) NULL,
    `settings` JSON NOT NULL,
    `plan` ENUM('free', 'pro', 'enterprise') NOT NULL DEFAULT 'free',
    `is_active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    UNIQUE INDEX `tenants_slug_key`(`slug`),
    UNIQUE INDEX `tenants_email_key`(`email`),
    UNIQUE INDEX `tenants_api_key_key`(`api_key`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `products` (
    `id` VARCHAR(36) NOT NULL,
    `tenant_id` VARCHAR(36) NOT NULL,
    `external_id` VARCHAR(255) NULL,
    `name` VARCHAR(500) NOT NULL,
    `description` TEXT NULL,
    `image_url` VARCHAR(500) NULL,
    `price` DECIMAL(12, 2) NULL,
    `starting_price` DECIMAL(12, 2) NULL,
    `min_bid_increment` DECIMAL(12, 2) NULL,
    `currency` VARCHAR(3) NOT NULL DEFAULT 'TRY',
    `stock_quantity` INTEGER NOT NULL DEFAULT 1,
    `status` ENUM('draft', 'ready', 'active', 'sold', 'cancelled') NOT NULL DEFAULT 'draft',
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `products_tenant_id_idx`(`tenant_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `live_sessions` (
    `id` VARCHAR(36) NOT NULL,
    `tenant_id` VARCHAR(36) NOT NULL,
    `ig_live_video_id` VARCHAR(100) NULL,
    `title` VARCHAR(255) NOT NULL,
    `status` ENUM('pending', 'live', 'ended') NOT NULL DEFAULT 'pending',
    `started_at` DATETIME(3) NULL,
    `ended_at` DATETIME(3) NULL,
    `total_viewers` INTEGER NOT NULL DEFAULT 0,
    `total_comments` INTEGER NOT NULL DEFAULT 0,
    `total_sales` INTEGER NOT NULL DEFAULT 0,
    `settings` JSON NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `live_sessions_tenant_id_idx`(`tenant_id`),
    INDEX `live_sessions_status_idx`(`status`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `auction_items` (
    `id` VARCHAR(36) NOT NULL,
    `live_session_id` VARCHAR(36) NOT NULL,
    `product_id` VARCHAR(36) NOT NULL,
    `mode` ENUM('fixed_price', 'auction') NOT NULL,
    `starting_price` DECIMAL(12, 2) NOT NULL,
    `current_price` DECIMAL(12, 2) NOT NULL,
    `min_bid_increment` DECIMAL(12, 2) NULL,
    `time_limit_seconds` INTEGER NULL,
    `status` ENUM('queued', 'active', 'countdown', 'sold', 'cancelled') NOT NULL DEFAULT 'queued',
    `activated_at` DATETIME(3) NULL,
    `closed_at` DATETIME(3) NULL,
    `winner_user_id` VARCHAR(36) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `auction_items_live_session_id_idx`(`live_session_id`),
    INDEX `auction_items_status_idx`(`status`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `bids` (
    `id` VARCHAR(36) NOT NULL,
    `auction_item_id` VARCHAR(36) NOT NULL,
    `user_id` VARCHAR(36) NULL,
    `ig_username` VARCHAR(100) NOT NULL,
    `ig_comment_id` VARCHAR(100) NULL,
    `bid_type` ENUM('claim', 'bid') NOT NULL,
    `amount` DECIMAL(12, 2) NOT NULL,
    `status` ENUM('pending', 'won', 'lost', 'cancelled') NOT NULL DEFAULT 'pending',
    `is_matched` BOOLEAN NOT NULL DEFAULT false,
    `comment_timestamp` DATETIME(3) NULL,
    `processed_at` DATETIME(3) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `bids_auction_item_id_idx`(`auction_item_id`),
    INDEX `bids_ig_username_idx`(`ig_username`),
    INDEX `bids_status_idx`(`status`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `users` (
    `id` VARCHAR(36) NOT NULL,
    `tenant_id` VARCHAR(36) NOT NULL,
    `email` VARCHAR(255) NULL,
    `ig_username` VARCHAR(100) NULL,
    `ig_user_id` VARCHAR(100) NULL,
    `ig_auth_method` ENUM('manual', 'oauth') NULL,
    `display_name` VARCHAR(200) NULL,
    `phone` VARCHAR(20) NULL,
    `external_user_id` VARCHAR(255) NULL,
    `is_verified` BOOLEAN NOT NULL DEFAULT false,
    `trust_score` DECIMAL(5, 2) NOT NULL DEFAULT 100,
    `total_purchases` INTEGER NOT NULL DEFAULT 0,
    `total_no_shows` INTEGER NOT NULL DEFAULT 0,
    `is_blocked` BOOLEAN NOT NULL DEFAULT false,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `users_tenant_id_idx`(`tenant_id`),
    INDEX `users_ig_username_idx`(`ig_username`),
    UNIQUE INDEX `users_tenant_id_ig_username_key`(`tenant_id`, `ig_username`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `cart_events` (
    `id` VARCHAR(36) NOT NULL,
    `tenant_id` VARCHAR(36) NOT NULL,
    `bid_id` VARCHAR(36) NOT NULL,
    `user_id` VARCHAR(36) NULL,
    `auction_item_id` VARCHAR(36) NOT NULL,
    `external_cart_id` VARCHAR(255) NULL,
    `status` ENUM('pending', 'sent', 'confirmed', 'failed') NOT NULL DEFAULT 'pending',
    `webhook_response` JSON NULL,
    `sent_at` DATETIME(3) NULL,
    `confirmed_at` DATETIME(3) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    UNIQUE INDEX `cart_events_bid_id_key`(`bid_id`),
    INDEX `cart_events_tenant_id_idx`(`tenant_id`),
    INDEX `cart_events_status_idx`(`status`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `products` ADD CONSTRAINT `products_tenant_id_fkey` FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `live_sessions` ADD CONSTRAINT `live_sessions_tenant_id_fkey` FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `auction_items` ADD CONSTRAINT `auction_items_live_session_id_fkey` FOREIGN KEY (`live_session_id`) REFERENCES `live_sessions`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `auction_items` ADD CONSTRAINT `auction_items_product_id_fkey` FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `auction_items` ADD CONSTRAINT `auction_items_winner_user_id_fkey` FOREIGN KEY (`winner_user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `bids` ADD CONSTRAINT `bids_auction_item_id_fkey` FOREIGN KEY (`auction_item_id`) REFERENCES `auction_items`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `bids` ADD CONSTRAINT `bids_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `users` ADD CONSTRAINT `users_tenant_id_fkey` FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cart_events` ADD CONSTRAINT `cart_events_tenant_id_fkey` FOREIGN KEY (`tenant_id`) REFERENCES `tenants`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cart_events` ADD CONSTRAINT `cart_events_bid_id_fkey` FOREIGN KEY (`bid_id`) REFERENCES `bids`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cart_events` ADD CONSTRAINT `cart_events_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cart_events` ADD CONSTRAINT `cart_events_auction_item_id_fkey` FOREIGN KEY (`auction_item_id`) REFERENCES `auction_items`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
