-- SPRINT 0 CORE FOUNDATION
CREATE TABLE IF NOT EXISTS tenants (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(191) NOT NULL,
  status ENUM('active','inactive') DEFAULT 'active',
  created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS licenses (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  location_limit INT DEFAULT 1,
  feature_flags LONGTEXT NULL,
  created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS locations (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  name VARCHAR(191) NOT NULL,
  is_active TINYINT(1) DEFAULT 1,
  created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS location_branding (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  location_id BIGINT UNSIGNED NOT NULL,
  logo_path VARCHAR(255) NULL,
  favicon_path VARCHAR(255) NULL,
  app_title VARCHAR(191) NULL,
  brand_name VARCHAR(191) NULL,
  sidebar_color VARCHAR(32) NULL,
  sidebar_menu_color VARCHAR(32) NULL,
  header_color VARCHAR(32) NULL,
  primary_button_color VARCHAR(32) NULL,
  secondary_color VARCHAR(32) NULL,
  footer_text VARCHAR(191) NULL,
  created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Seed minimal
INSERT INTO tenants (id,name,status) VALUES (1,'Default Tenant','active')
ON DUPLICATE KEY UPDATE name=name;

INSERT INTO licenses (tenant_id,location_limit,feature_flags)
VALUES (1,1,'{}');

INSERT INTO locations (tenant_id,name,is_active)
VALUES (1,'Main Location',1);

INSERT INTO location_branding (tenant_id,location_id,app_title,brand_name,header_color,primary_button_color,footer_text)
VALUES (1,1,'Aplikasi Percetakan','Rajawali Printing','#e11d48','#e11d48','© Rajawali Printing - 2026');