-- CreateTable
CREATE TABLE `AdminRefreshToken` (
    `id` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `tokenHash` VARCHAR(191) NOT NULL,
    `expiresAt` DATETIME(3) NOT NULL,
    `revoked` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `AdminRefreshToken_tokenHash_key`(`tokenHash`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `ShopUserRefreshToken` (
    `id` VARCHAR(191) NOT NULL,
    `shopUserId` VARCHAR(191) NOT NULL,
    `tokenHash` VARCHAR(191) NOT NULL,
    `expiresAt` DATETIME(3) NOT NULL,
    `revoked` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `ShopUserRefreshToken_tokenHash_key`(`tokenHash`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `users` (
    `id` VARCHAR(191) NOT NULL,
    `email` VARCHAR(191) NOT NULL,
    `password` VARCHAR(191) NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `users_email_key`(`email`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `shops` (
    `id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `owner_id` VARCHAR(191) NOT NULL,
    `revision` INTEGER NOT NULL DEFAULT 1,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `shops_owner_id_idx`(`owner_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `shop_user_stations` (
    `id` VARCHAR(191) NOT NULL,
    `shop_user_id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `shop_user_stations_shop_user_id_station_id_key`(`shop_user_id`, `station_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `shop_users` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `email` VARCHAR(191) NOT NULL,
    `display_name` VARCHAR(191) NULL,
    `username` VARCHAR(191) NOT NULL,
    `password` VARCHAR(191) NOT NULL,
    `must_change_password` BOOLEAN NOT NULL DEFAULT true,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `role` ENUM('MANAGER', 'CASHIER', 'STAFF', 'RUNNER') NOT NULL,
    `revision` INTEGER NOT NULL DEFAULT 1,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `shop_users_shop_id_idx`(`shop_id`),
    INDEX `shop_users_email_idx`(`email`),
    UNIQUE INDEX `shop_users_shop_id_email_key`(`shop_id`, `email`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `categories` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `color` VARCHAR(191) NULL,
    `image_url` VARCHAR(191) NULL,
    `display_order` INTEGER NOT NULL DEFAULT 0,
    `active` BOOLEAN NOT NULL DEFAULT true,

    INDEX `categories_shop_id_active_idx`(`shop_id`, `active`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `products` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `price` INTEGER NOT NULL,
    `vat_rate` INTEGER NOT NULL,
    `image_url` VARCHAR(191) NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `out_of_stock` BOOLEAN NOT NULL DEFAULT false,
    `type` ENUM('STANDARD', 'FOOD') NOT NULL DEFAULT 'STANDARD',
    `revision` INTEGER NOT NULL DEFAULT 1,
    `category_id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NULL,
    `recipe_id` VARCHAR(191) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `products_shop_id_category_id_idx`(`shop_id`, `category_id`),
    INDEX `products_station_id_idx`(`station_id`),
    INDEX `products_recipe_id_idx`(`recipe_id`),
    INDEX `products_shop_id_active_idx`(`shop_id`, `active`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `payments` (
    `id` VARCHAR(191) NOT NULL,
    `order_id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NULL,
    `payment_attempt_id` VARCHAR(191) NULL,
    `method` ENUM('CASH', 'CARD', 'CHECK', 'APPLE_PAY', 'GOOGLE_PAY', 'GIFT_CARD', 'VOUCHER') NOT NULL,
    `execution_method` ENUM('MANUAL', 'READER', 'TAP_TO_PAY', 'HOSTED', 'QR_CODE') NULL,
    `provider` ENUM('SUMUP', 'STRIPE', 'SQUARE', 'ADYEN') NULL,
    `account_payment_provider_id` VARCHAR(191) NULL,
    `provider_device_id` VARCHAR(191) NULL,
    `shop_payment_method_config_id` VARCHAR(191) NULL,
    `station_payment_method_config_id` VARCHAR(191) NULL,
    `amount` INTEGER NOT NULL,
    `reference` VARCHAR(191) NULL,
    `config_snapshot` JSON NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `payments_payment_attempt_id_key`(`payment_attempt_id`),
    INDEX `payments_order_id_idx`(`order_id`),
    INDEX `payments_shop_id_created_at_idx`(`shop_id`, `created_at`),
    INDEX `payments_station_id_idx`(`station_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `orders` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `total` INTEGER NOT NULL,
    `discount_total` INTEGER NOT NULL DEFAULT 0,
    `status` ENUM('DRAFT', 'PREPARING', 'READY', 'PENDING_PAYMENT', 'PARTIALLY_PAID', 'PAID', 'CANCELLED') NOT NULL DEFAULT 'DRAFT',
    `revision` INTEGER NOT NULL DEFAULT 1,
    `created_by_id` VARCHAR(191) NOT NULL,
    `stationId` VARCHAR(191) NULL,
    `paid_at` DATETIME(3) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `cash_session_id` VARCHAR(191) NULL,
    `session_order_number` INTEGER NULL,
    `userId` VARCHAR(191) NULL,

    INDEX `orders_shop_id_status_idx`(`shop_id`, `status`),
    INDEX `orders_shop_id_created_at_idx`(`shop_id`, `created_at`),
    INDEX `orders_cash_session_id_idx`(`cash_session_id`),
    INDEX `orders_created_by_id_idx`(`created_by_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `order_items` (
    `id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `quantity` INTEGER NOT NULL,
    `unit_price` INTEGER NOT NULL,
    `status` ENUM('PENDING', 'PREPARING', 'READY', 'PICKED_UP', 'DELIVERED', 'CANCELLED') NOT NULL DEFAULT 'PENDING',
    `revision` INTEGER NOT NULL DEFAULT 1,
    `order_id` VARCHAR(191) NOT NULL,
    `product_id` VARCHAR(191) NOT NULL,
    `recipe_id` VARCHAR(191) NULL,
    `recipe_name` VARCHAR(191) NULL,
    `taken_by_id` VARCHAR(191) NULL,
    `picked_up_by_id` VARCHAR(191) NULL,
    `transfer_route_id` VARCHAR(191) NULL,
    `transported_by_id` VARCHAR(191) NULL,
    `transported_by_name` VARCHAR(191) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `order_items_order_id_idx`(`order_id`),
    INDEX `order_items_order_id_status_idx`(`order_id`, `status`),
    INDEX `order_items_product_id_idx`(`product_id`),
    INDEX `order_items_taken_by_id_idx`(`taken_by_id`),
    INDEX `order_items_picked_up_by_id_idx`(`picked_up_by_id`),
    INDEX `order_items_transfer_route_id_idx`(`transfer_route_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `account_payment_providers` (
    `id` VARCHAR(191) NOT NULL,
    `owner_id` VARCHAR(191) NOT NULL,
    `provider` ENUM('SUMUP', 'STRIPE', 'SQUARE', 'ADYEN') NOT NULL,
    `label` VARCHAR(191) NULL,
    `config` JSON NULL,
    `secrets` JSON NULL,
    `metadata` JSON NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `account_payment_providers_owner_id_active_idx`(`owner_id`, `active`),
    INDEX `account_payment_providers_owner_id_provider_idx`(`owner_id`, `provider`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `provider_devices` (
    `id` VARCHAR(191) NOT NULL,
    `account_payment_provider_id` VARCHAR(191) NOT NULL,
    `external_id` VARCHAR(191) NOT NULL,
    `label` VARCHAR(191) NULL,
    `metadata` JSON NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,

    INDEX `provider_devices_account_payment_provider_id_idx`(`account_payment_provider_id`),
    INDEX `provider_devices_active_idx`(`active`),
    UNIQUE INDEX `provider_devices_account_payment_provider_id_external_id_key`(`account_payment_provider_id`, `external_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `shop_payment_method_configs` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `method` ENUM('CASH', 'CARD', 'CHECK', 'APPLE_PAY', 'GOOGLE_PAY', 'GIFT_CARD', 'VOUCHER') NOT NULL,
    `enabled` BOOLEAN NOT NULL DEFAULT true,
    `display_order` INTEGER NOT NULL DEFAULT 0,
    `revision` INTEGER NOT NULL DEFAULT 1,
    `account_payment_provider_id` VARCHAR(191) NULL,
    `default_provider_device_id` VARCHAR(191) NULL,
    `metadata` JSON NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `shop_payment_method_configs_shop_id_enabled_idx`(`shop_id`, `enabled`),
    UNIQUE INDEX `shop_payment_method_configs_shop_id_method_key`(`shop_id`, `method`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `station_payment_method_configs` (
    `id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NOT NULL,
    `shop_payment_method_config_id` VARCHAR(191) NOT NULL,
    `enabled` BOOLEAN NOT NULL DEFAULT true,
    `display_order` INTEGER NOT NULL DEFAULT 0,
    `is_default` BOOLEAN NOT NULL DEFAULT false,
    `metadata` JSON NULL,
    `provider_device_id` VARCHAR(191) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `station_payment_method_configs_station_id_enabled_idx`(`station_id`, `enabled`),
    UNIQUE INDEX `station_payment_method_configs_station_id_shop_payment_metho_key`(`station_id`, `shop_payment_method_config_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `station_payment_bindings` (
    `id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NOT NULL,
    `provider_device_id` VARCHAR(191) NOT NULL,
    `is_default` BOOLEAN NOT NULL DEFAULT true,

    INDEX `station_payment_bindings_station_id_idx`(`station_id`),
    INDEX `station_payment_bindings_provider_device_id_idx`(`provider_device_id`),
    UNIQUE INDEX `station_payment_bindings_station_id_provider_device_id_key`(`station_id`, `provider_device_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `stations` (
    `id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `main_type` ENUM('CASH', 'PRODUCTION', 'TRANSFORMATION', 'MOBILE', 'ADMIN') NOT NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `revision` INTEGER NOT NULL DEFAULT 1,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `stations_shop_id_active_idx`(`shop_id`, `active`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `station_product_stocks` (
    `id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NOT NULL,
    `product_id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `quantity` INTEGER NOT NULL DEFAULT 0,
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `station_product_stocks_shop_id_idx`(`shop_id`),
    UNIQUE INDEX `station_product_stocks_station_id_product_id_key`(`station_id`, `product_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `StationCapability` (
    `id` VARCHAR(191) NOT NULL,
    `code` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `StationCapability_code_key`(`code`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `station_capability_assignments` (
    `id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NOT NULL,
    `capability_id` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `station_capability_assignments_station_id_capability_id_key`(`station_id`, `capability_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `production_requests` (
    `id` VARCHAR(191) NOT NULL,
    `product_id` VARCHAR(191) NOT NULL,
    `quantity` INTEGER NOT NULL,
    `status` ENUM('PENDING', 'PREPARING', 'READY', 'COMPLETED') NOT NULL DEFAULT 'PENDING',
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `production_requests_product_id_idx`(`product_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `product_stocks` (
    `id` VARCHAR(191) NOT NULL,
    `product_id` VARCHAR(191) NOT NULL,
    `available` INTEGER NOT NULL DEFAULT 0,

    UNIQUE INDEX `product_stocks_product_id_key`(`product_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `cash_sessions` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NULL,
    `opened_by_id` VARCHAR(191) NOT NULL,
    `closed_by_id` VARCHAR(191) NULL,
    `opened_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `closed_at` DATETIME(3) NULL,
    `status` ENUM('OPEN', 'CLOSED') NOT NULL DEFAULT 'OPEN',
    `revision` INTEGER NOT NULL DEFAULT 1,
    `opening_cash` INTEGER NOT NULL DEFAULT 0,
    `closing_cash` INTEGER NULL,
    `next_order_number` INTEGER NOT NULL DEFAULT 1,

    INDEX `cash_sessions_shop_id_status_idx`(`shop_id`, `status`),
    INDEX `cash_sessions_station_id_status_idx`(`station_id`, `status`),
    INDEX `cash_sessions_opened_by_id_idx`(`opened_by_id`),
    INDEX `cash_sessions_closed_by_id_idx`(`closed_by_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `payment_attempts` (
    `id` VARCHAR(191) NOT NULL,
    `order_id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `station_id` VARCHAR(191) NULL,
    `method` ENUM('CASH', 'CARD', 'CHECK', 'APPLE_PAY', 'GOOGLE_PAY', 'GIFT_CARD', 'VOUCHER') NOT NULL,
    `execution_method` ENUM('MANUAL', 'READER', 'TAP_TO_PAY', 'HOSTED', 'QR_CODE') NULL,
    `provider` ENUM('SUMUP', 'STRIPE', 'SQUARE', 'ADYEN') NULL,
    `account_payment_provider_id` VARCHAR(191) NULL,
    `provider_device_id` VARCHAR(191) NULL,
    `shop_payment_method_config_id` VARCHAR(191) NULL,
    `station_payment_method_config_id` VARCHAR(191) NULL,
    `amount` INTEGER NOT NULL,
    `currency` VARCHAR(191) NOT NULL DEFAULT 'EUR',
    `provider_checkout_id` VARCHAR(191) NULL,
    `provider_tx_id` VARCHAR(191) NULL,
    `status` ENUM('PENDING', 'PAID', 'FAILED', 'CANCELLED') NOT NULL DEFAULT 'PENDING',
    `failure_reason` VARCHAR(191) NULL,
    `config_snapshot` JSON NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    UNIQUE INDEX `payment_attempts_provider_checkout_id_key`(`provider_checkout_id`),
    INDEX `payment_attempts_order_id_idx`(`order_id`),
    INDEX `payment_attempts_shop_id_status_idx`(`shop_id`, `status`),
    INDEX `payment_attempts_station_id_status_idx`(`station_id`, `status`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `shop_event_seqs` (
    `shop_id` VARCHAR(191) NOT NULL,
    `seq` BIGINT NOT NULL DEFAULT 0,
    `updated_at` DATETIME(3) NOT NULL,

    PRIMARY KEY (`shop_id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `shop_events` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `seq` BIGINT NOT NULL,
    `type` VARCHAR(191) NOT NULL,
    `entity` VARCHAR(191) NULL,
    `entity_id` VARCHAR(191) NULL,
    `revision` INTEGER NULL,
    `payload` JSON NOT NULL,
    `actor_user_id` VARCHAR(191) NULL,
    `device_id` VARCHAR(191) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `dispatched_at` DATETIME(3) NULL,

    INDEX `shop_events_shop_id_seq_idx`(`shop_id`, `seq`),
    INDEX `shop_events_shop_id_created_at_idx`(`shop_id`, `created_at`),
    INDEX `shop_events_shop_id_entity_entity_id_idx`(`shop_id`, `entity`, `entity_id`),
    UNIQUE INDEX `shop_events_shop_id_seq_key`(`shop_id`, `seq`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `promo_codes` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `code` VARCHAR(191) NOT NULL,
    `label` VARCHAR(191) NULL,
    `discount_type` ENUM('PERCENTAGE', 'FIXED_AMOUNT', 'FREE_PRODUCT') NOT NULL,
    `discount_value` INTEGER NOT NULL,
    `target_product_id` VARCHAR(191) NULL,
    `target_category_id` VARCHAR(191) NULL,
    `max_uses` INTEGER NULL,
    `max_uses_per_order` INTEGER NOT NULL DEFAULT 1,
    `current_uses` INTEGER NOT NULL DEFAULT 0,
    `starts_at` DATETIME(3) NULL,
    `expires_at` DATETIME(3) NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `promo_codes_shop_id_active_idx`(`shop_id`, `active`),
    INDEX `promo_codes_shop_id_code_idx`(`shop_id`, `code`),
    UNIQUE INDEX `promo_codes_shop_id_code_key`(`shop_id`, `code`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `promo_code_usages` (
    `id` VARCHAR(191) NOT NULL,
    `promo_code_id` VARCHAR(191) NOT NULL,
    `order_id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `discount_applied` INTEGER NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `promo_code_usages_promo_code_id_idx`(`promo_code_id`),
    INDEX `promo_code_usages_order_id_idx`(`order_id`),
    INDEX `promo_code_usages_shop_id_idx`(`shop_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `ingredients` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `allergen` BOOLEAN NOT NULL DEFAULT false,
    `extra_price` INTEGER NOT NULL DEFAULT 0,
    `image_url` VARCHAR(191) NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `ingredients_shop_id_active_idx`(`shop_id`, `active`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `recipes` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `description` VARCHAR(191) NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `recipes_shop_id_active_idx`(`shop_id`, `active`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `recipe_ingredients` (
    `id` VARCHAR(191) NOT NULL,
    `recipe_id` VARCHAR(191) NOT NULL,
    `ingredient_id` VARCHAR(191) NOT NULL,
    `role` ENUM('BASE', 'DEFAULT', 'SUPPLEMENT') NOT NULL DEFAULT 'DEFAULT',
    `default_quantity` INTEGER NOT NULL DEFAULT 1,
    `max_quantity` INTEGER NULL,
    `extra_price` INTEGER NOT NULL DEFAULT 0,

    INDEX `recipe_ingredients_recipe_id_idx`(`recipe_id`),
    INDEX `recipe_ingredients_ingredient_id_idx`(`ingredient_id`),
    UNIQUE INDEX `recipe_ingredients_recipe_id_ingredient_id_key`(`recipe_id`, `ingredient_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `transfer_routes` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `from_station_id` VARCHAR(191) NOT NULL,
    `to_station_id` VARCHAR(191) NOT NULL,
    `handler_mode` ENUM('RUNNER', 'SELF_CASHIER', 'SOURCE_STATION') NOT NULL DEFAULT 'RUNNER',
    `assigned_runner_id` VARCHAR(191) NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    INDEX `transfer_routes_shop_id_active_idx`(`shop_id`, `active`),
    INDEX `transfer_routes_assigned_runner_id_idx`(`assigned_runner_id`),
    UNIQUE INDEX `transfer_routes_from_station_id_to_station_id_key`(`from_station_id`, `to_station_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `order_item_customizations` (
    `id` VARCHAR(191) NOT NULL,
    `order_item_id` VARCHAR(191) NOT NULL,
    `ingredient_id` VARCHAR(191) NULL,
    `ingredient_name` VARCHAR(191) NOT NULL,
    `action` ENUM('ADDED', 'REMOVED') NOT NULL,
    `quantity` INTEGER NOT NULL DEFAULT 1,
    `extra_price` INTEGER NOT NULL DEFAULT 0,

    INDEX `order_item_customizations_order_item_id_idx`(`order_item_id`),
    INDEX `order_item_customizations_ingredient_id_idx`(`ingredient_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `order_item_lifecycle_events` (
    `id` VARCHAR(191) NOT NULL,
    `order_item_id` VARCHAR(191) NOT NULL,
    `event_type` ENUM('ACKNOWLEDGED', 'READY', 'PICKED_UP', 'DELIVERED', 'CANCELLED', 'REVERTED') NOT NULL,
    `from_status` ENUM('PENDING', 'PREPARING', 'READY', 'PICKED_UP', 'DELIVERED', 'CANCELLED') NOT NULL,
    `to_status` ENUM('PENDING', 'PREPARING', 'READY', 'PICKED_UP', 'DELIVERED', 'CANCELLED') NOT NULL,
    `actor_id` VARCHAR(191) NULL,
    `actor_name` VARCHAR(191) NULL,
    `station_id` VARCHAR(191) NULL,
    `occurred_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `order_item_lifecycle_events_order_item_id_idx`(`order_item_id`),
    INDEX `order_item_lifecycle_events_actor_id_idx`(`actor_id`),
    INDEX `order_item_lifecycle_events_order_item_id_event_type_idx`(`order_item_id`, `event_type`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `AdminRefreshToken` ADD CONSTRAINT `AdminRefreshToken_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `ShopUserRefreshToken` ADD CONSTRAINT `ShopUserRefreshToken_shopUserId_fkey` FOREIGN KEY (`shopUserId`) REFERENCES `shop_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `shops` ADD CONSTRAINT `shops_owner_id_fkey` FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `shop_user_stations` ADD CONSTRAINT `shop_user_stations_shop_user_id_fkey` FOREIGN KEY (`shop_user_id`) REFERENCES `shop_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `shop_user_stations` ADD CONSTRAINT `shop_user_stations_station_id_fkey` FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `shop_users` ADD CONSTRAINT `shop_users_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `categories` ADD CONSTRAINT `categories_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `products` ADD CONSTRAINT `products_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `products` ADD CONSTRAINT `products_category_id_fkey` FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `products` ADD CONSTRAINT `products_station_id_fkey` FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `products` ADD CONSTRAINT `products_recipe_id_fkey` FOREIGN KEY (`recipe_id`) REFERENCES `recipes`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payments` ADD CONSTRAINT `payments_order_id_fkey` FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payments` ADD CONSTRAINT `payments_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payments` ADD CONSTRAINT `payments_station_id_fkey` FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payments` ADD CONSTRAINT `payments_payment_attempt_id_fkey` FOREIGN KEY (`payment_attempt_id`) REFERENCES `payment_attempts`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payments` ADD CONSTRAINT `payments_account_payment_provider_id_fkey` FOREIGN KEY (`account_payment_provider_id`) REFERENCES `account_payment_providers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payments` ADD CONSTRAINT `payments_provider_device_id_fkey` FOREIGN KEY (`provider_device_id`) REFERENCES `provider_devices`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payments` ADD CONSTRAINT `payments_shop_payment_method_config_id_fkey` FOREIGN KEY (`shop_payment_method_config_id`) REFERENCES `shop_payment_method_configs`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payments` ADD CONSTRAINT `payments_station_payment_method_config_id_fkey` FOREIGN KEY (`station_payment_method_config_id`) REFERENCES `station_payment_method_configs`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `orders` ADD CONSTRAINT `orders_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `orders` ADD CONSTRAINT `orders_created_by_id_fkey` FOREIGN KEY (`created_by_id`) REFERENCES `shop_users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `orders` ADD CONSTRAINT `orders_stationId_fkey` FOREIGN KEY (`stationId`) REFERENCES `stations`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `orders` ADD CONSTRAINT `orders_cash_session_id_fkey` FOREIGN KEY (`cash_session_id`) REFERENCES `cash_sessions`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `orders` ADD CONSTRAINT `orders_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `order_items` ADD CONSTRAINT `order_items_order_id_fkey` FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE `order_items` ADD CONSTRAINT `order_items_taken_by_id_fkey` FOREIGN KEY (`taken_by_id`) REFERENCES `shop_users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `order_items` ADD CONSTRAINT `order_items_picked_up_by_id_fkey` FOREIGN KEY (`picked_up_by_id`) REFERENCES `shop_users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `order_items` ADD CONSTRAINT `order_items_transfer_route_id_fkey` FOREIGN KEY (`transfer_route_id`) REFERENCES `transfer_routes`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `account_payment_providers` ADD CONSTRAINT `account_payment_providers_owner_id_fkey` FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `provider_devices` ADD CONSTRAINT `provider_devices_account_payment_provider_id_fkey` FOREIGN KEY (`account_payment_provider_id`) REFERENCES `account_payment_providers`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `shop_payment_method_configs` ADD CONSTRAINT `shop_payment_method_configs_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `shop_payment_method_configs` ADD CONSTRAINT `shop_payment_method_configs_account_payment_provider_id_fkey` FOREIGN KEY (`account_payment_provider_id`) REFERENCES `account_payment_providers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `shop_payment_method_configs` ADD CONSTRAINT `shop_payment_method_configs_default_provider_device_id_fkey` FOREIGN KEY (`default_provider_device_id`) REFERENCES `provider_devices`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `station_payment_method_configs` ADD CONSTRAINT `station_payment_method_configs_station_id_fkey` FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `station_payment_method_configs` ADD CONSTRAINT `station_payment_method_configs_shop_payment_method_config_i_fkey` FOREIGN KEY (`shop_payment_method_config_id`) REFERENCES `shop_payment_method_configs`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `station_payment_method_configs` ADD CONSTRAINT `station_payment_method_configs_provider_device_id_fkey` FOREIGN KEY (`provider_device_id`) REFERENCES `provider_devices`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `station_payment_bindings` ADD CONSTRAINT `station_payment_bindings_station_id_fkey` FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `station_payment_bindings` ADD CONSTRAINT `station_payment_bindings_provider_device_id_fkey` FOREIGN KEY (`provider_device_id`) REFERENCES `provider_devices`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `stations` ADD CONSTRAINT `stations_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `station_product_stocks` ADD CONSTRAINT `station_product_stocks_station_id_fkey` FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE `station_product_stocks` ADD CONSTRAINT `station_product_stocks_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `station_capability_assignments` ADD CONSTRAINT `station_capability_assignments_station_id_fkey` FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `station_capability_assignments` ADD CONSTRAINT `station_capability_assignments_capability_id_fkey` FOREIGN KEY (`capability_id`) REFERENCES `StationCapability`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE `cash_sessions` ADD CONSTRAINT `cash_sessions_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cash_sessions` ADD CONSTRAINT `cash_sessions_station_id_fkey` FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cash_sessions` ADD CONSTRAINT `cash_sessions_opened_by_id_fkey` FOREIGN KEY (`opened_by_id`) REFERENCES `shop_users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cash_sessions` ADD CONSTRAINT `cash_sessions_closed_by_id_fkey` FOREIGN KEY (`closed_by_id`) REFERENCES `shop_users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payment_attempts` ADD CONSTRAINT `payment_attempts_order_id_fkey` FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payment_attempts` ADD CONSTRAINT `payment_attempts_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payment_attempts` ADD CONSTRAINT `payment_attempts_station_id_fkey` FOREIGN KEY (`station_id`) REFERENCES `stations`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payment_attempts` ADD CONSTRAINT `payment_attempts_account_payment_provider_id_fkey` FOREIGN KEY (`account_payment_provider_id`) REFERENCES `account_payment_providers`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payment_attempts` ADD CONSTRAINT `payment_attempts_provider_device_id_fkey` FOREIGN KEY (`provider_device_id`) REFERENCES `provider_devices`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payment_attempts` ADD CONSTRAINT `payment_attempts_shop_payment_method_config_id_fkey` FOREIGN KEY (`shop_payment_method_config_id`) REFERENCES `shop_payment_method_configs`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `payment_attempts` ADD CONSTRAINT `payment_attempts_station_payment_method_config_id_fkey` FOREIGN KEY (`station_payment_method_config_id`) REFERENCES `station_payment_method_configs`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `shop_event_seqs` ADD CONSTRAINT `shop_event_seqs_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `shop_events` ADD CONSTRAINT `shop_events_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `promo_codes` ADD CONSTRAINT `promo_codes_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `promo_codes` ADD CONSTRAINT `promo_codes_target_product_id_fkey` FOREIGN KEY (`target_product_id`) REFERENCES `products`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `promo_codes` ADD CONSTRAINT `promo_codes_target_category_id_fkey` FOREIGN KEY (`target_category_id`) REFERENCES `categories`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `promo_code_usages` ADD CONSTRAINT `promo_code_usages_promo_code_id_fkey` FOREIGN KEY (`promo_code_id`) REFERENCES `promo_codes`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `promo_code_usages` ADD CONSTRAINT `promo_code_usages_order_id_fkey` FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `promo_code_usages` ADD CONSTRAINT `promo_code_usages_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `ingredients` ADD CONSTRAINT `ingredients_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `recipes` ADD CONSTRAINT `recipes_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `recipe_ingredients` ADD CONSTRAINT `recipe_ingredients_recipe_id_fkey` FOREIGN KEY (`recipe_id`) REFERENCES `recipes`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `recipe_ingredients` ADD CONSTRAINT `recipe_ingredients_ingredient_id_fkey` FOREIGN KEY (`ingredient_id`) REFERENCES `ingredients`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transfer_routes` ADD CONSTRAINT `transfer_routes_shop_id_fkey` FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transfer_routes` ADD CONSTRAINT `transfer_routes_from_station_id_fkey` FOREIGN KEY (`from_station_id`) REFERENCES `stations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transfer_routes` ADD CONSTRAINT `transfer_routes_to_station_id_fkey` FOREIGN KEY (`to_station_id`) REFERENCES `stations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `transfer_routes` ADD CONSTRAINT `transfer_routes_assigned_runner_id_fkey` FOREIGN KEY (`assigned_runner_id`) REFERENCES `shop_users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `order_item_customizations` ADD CONSTRAINT `order_item_customizations_order_item_id_fkey` FOREIGN KEY (`order_item_id`) REFERENCES `order_items`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `order_item_customizations` ADD CONSTRAINT `order_item_customizations_ingredient_id_fkey` FOREIGN KEY (`ingredient_id`) REFERENCES `ingredients`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `order_item_lifecycle_events` ADD CONSTRAINT `order_item_lifecycle_events_order_item_id_fkey` FOREIGN KEY (`order_item_id`) REFERENCES `order_items`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `order_item_lifecycle_events` ADD CONSTRAINT `order_item_lifecycle_events_actor_id_fkey` FOREIGN KEY (`actor_id`) REFERENCES `shop_users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
