-- CreateTable
CREATE TABLE `pos_push_installations` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `shop_user_id` VARCHAR(191) NULL,
    `terminal_id` VARCHAR(191) NOT NULL,
    `fcm_token` VARCHAR(512) NOT NULL,
    `platform` ENUM('ANDROID') NOT NULL,
    `app_version` VARCHAR(191) NULL,
    `admin_enabled` BOOLEAN NOT NULL DEFAULT true,
    `is_active` BOOLEAN NOT NULL DEFAULT true,
    `app_state` ENUM('FOREGROUND', 'BACKGROUND', 'UNKNOWN') NOT NULL DEFAULT 'UNKNOWN',
    `last_seen_at` DATETIME(3) NOT NULL,
    `last_foreground_at` DATETIME(3) NULL,
    `last_push_sent_at` DATETIME(3) NULL,
    `last_push_event` VARCHAR(191) NULL,
    `last_firebase_error_code` VARCHAR(191) NULL,
    `last_firebase_error_at` DATETIME(3) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    UNIQUE INDEX `pos_push_installations_terminal_id_key`(`terminal_id`),
    UNIQUE INDEX `pos_push_installations_fcm_token_key`(`fcm_token`),
    INDEX `pos_push_installations_shop_id_shop_user_id_is_active_idx`(`shop_id`, `shop_user_id`, `is_active`),
    INDEX `pos_push_installations_shop_id_is_active_app_state_idx`(`shop_id`, `is_active`, `app_state`),
    INDEX `pos_push_installations_last_seen_at_idx`(`last_seen_at`),
    INDEX `pos_push_installations_shop_user_id_last_seen_at_idx`(`shop_user_id`, `last_seen_at`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

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

-- AddForeignKey
ALTER TABLE `pos_push_installations` ADD CONSTRAINT `pos_push_installations_shop_user_id_fkey` FOREIGN KEY (`shop_user_id`) REFERENCES `shop_users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
