

CREATE TABLE IF NOT EXISTS roles (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  tenant_id BIGINT UNSIGNED NOT NULL,
  name VARCHAR(80) NOT NULL,
  code VARCHAR(80) NOT NULL,
  is_system TINYINT(1) NOT NULL DEFAULT 0,
  created_at DATETIME NULL,
  updated_at DATETIME NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_roles_tenant_code (tenant_id, code),
  KEY idx_roles_tenant (tenant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS permissions (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  code VARCHAR(120) NOT NULL,
  name VARCHAR(160) NOT NULL,
  module VARCHAR(80) NOT NULL,
  created_at DATETIME NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_permissions_code (code),
  KEY idx_permissions_module (module)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS role_permissions (
  role_id BIGINT UNSIGNED NOT NULL,
  permission_id BIGINT UNSIGNED NOT NULL,
  PRIMARY KEY (role_id, permission_id),
  CONSTRAINT fk_role_permissions_role FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE,
  CONSTRAINT fk_role_permissions_perm FOREIGN KEY (permission_id) REFERENCES permissions(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS user_roles (
  user_id BIGINT UNSIGNED NOT NULL,
  role_id BIGINT UNSIGNED NOT NULL,
  PRIMARY KEY (user_id, role_id),
  CONSTRAINT fk_user_roles_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  CONSTRAINT fk_user_roles_role FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS user_locations (
  user_id BIGINT UNSIGNED NOT NULL,
  location_id BIGINT UNSIGNED NOT NULL,
  PRIMARY KEY (user_id, location_id),
  CONSTRAINT fk_user_locations_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  CONSTRAINT fk_user_locations_location FOREIGN KEY (location_id) REFERENCES locations(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================
-- Seed (minimal)
-- ============================================================

INSERT IGNORE INTO permissions (code, name, module, created_at) VALUES
('pos.view','Lihat POS','pos',NOW()),
('pos.add','Tambah POS','pos',NOW()),
('products.view','Lihat Produk','products',NOW()),
('products.add','Tambah Produk','products',NOW()),
('products.edit','Edit Produk','products',NOW()),
('customers.view','Lihat Customer','customers',NOW()),
('customers.add','Tambah Customer','customers',NOW()),
('customers.edit','Edit Customer','customers',NOW()),
('settings.view','Lihat Settings','settings',NOW()),
('settings.users','Kelola Users','settings',NOW()),
('settings.roles','Kelola Roles & Permissions','settings',NOW()),
('reports.view','Lihat Report','reports',NOW()),
('reports.export','Export Report','reports',NOW());

-- roles system (tenant_id=1 contoh)
INSERT IGNORE INTO roles (tenant_id, name, code, is_system, created_at) VALUES
(1,'Owner','owner',1,NOW()),
(1,'Admin','admin',1,NOW()),
(1,'Kepala Toko','kepala_toko',1,NOW()),
(1,'Kasir','kasir',1,NOW()),
(1,'Karyawan','karyawan',1,NOW());

-- NOTE: mapping permission ke role akan kamu set via UI (accordion checklist) nanti,
-- tapi untuk awal bisa kasih admin full, kasir pos.view saja, dll.
