-- ============================================================
-- FashCognitive - DSP Mobile Receiving (Phase 3)
-- Lightweight device tokens for the warehouse receiving PWA.
-- Each token identifies a registered device/tablet; the mobile
-- PWA passes it as ?token= (mirrors the supplier portal pattern).
-- ============================================================

CREATE TABLE IF NOT EXISTS `dsp_device_tokens` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `token` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `company_id` int unsigned NOT NULL DEFAULT 1,
  `active` tinyint(1) NOT NULL DEFAULT 1,
  `last_used_at` datetime DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_dt_token` (`token`),
  KEY `idx_dt_company` (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Seed the demo warehouse tablet (dev/demo only).
INSERT INTO `dsp_device_tokens` (`token`, `name`, `company_id`, `active`)
SELECT 'd7f3a9c2e8b14f6d9a0c3e5b7f2a8d4c', 'Warehouse Gate Tablet 1 (demo)', 1, 1
WHERE NOT EXISTS (SELECT 1 FROM `dsp_device_tokens` WHERE `token` = 'd7f3a9c2e8b14f6d9a0c3e5b7f2a8d4c');
