-- 권한/메뉴 관리 (추후 ERP 확장용)
BEGIN;

CREATE TABLE IF NOT EXISTS momo_roles (
  objid       TEXT PRIMARY KEY,
  role_code   VARCHAR(50) NOT NULL UNIQUE,
  role_name   VARCHAR(100) NOT NULL,
  description VARCHAR(300),
  is_system   CHAR(1) DEFAULT 'N',     -- 'Y'면 시스템 기본권한 (수정/삭제 불가)
  is_del      CHAR(1) DEFAULT 'N',
  regdate     TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS momo_menus (
  objid       TEXT PRIMARY KEY,
  menu_code   VARCHAR(50) NOT NULL UNIQUE,
  menu_name   VARCHAR(100) NOT NULL,
  menu_url    VARCHAR(200),
  icon_name   VARCHAR(50),
  parent_code VARCHAR(50),
  sort_order  INT DEFAULT 0,
  group_name  VARCHAR(50),
  is_system   CHAR(1) DEFAULT 'N',
  is_del      CHAR(1) DEFAULT 'N',
  regdate     TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS momo_role_menus (
  role_code   VARCHAR(50) NOT NULL,
  menu_code   VARCHAR(50) NOT NULL,
  can_read    CHAR(1) DEFAULT 'Y',
  can_write   CHAR(1) DEFAULT 'N',
  PRIMARY KEY (role_code, menu_code)
);

-- 시스템 기본 권한 시드
INSERT INTO momo_roles (objid, role_code, role_name, description, is_system) VALUES
  ('ROLE_USER',  'USER',  '거래처',     '품목 검색 및 출고 요청만 가능', 'Y'),
  ('ROLE_ADMIN', 'ADMIN', '관리자',     '모든 메뉴 접근 + 승인/정산', 'Y')
ON CONFLICT (role_code) DO NOTHING;

-- 메뉴 마스터 시드 (현재 사이드바와 동일 구조)
INSERT INTO momo_menus (objid, menu_code, menu_name, menu_url, group_name, sort_order, is_system) VALUES
  ('M_DASH',  'DASHBOARD',  '대시보드',     '/m/dashboard',         '_top',     1, 'Y'),
  ('M_ITEMS', 'ITEMS',      '품목 검색',    '/m/items',             '주문',     10, 'Y'),
  ('M_ONEW',  'ORDER_NEW',  '출고 요청',    '/m/orders/new',        '주문',     11, 'Y'),
  ('M_OLIST', 'ORDER_LIST', '내 출고 이력', '/m/orders',            '주문',     12, 'Y'),
  ('M_AITEM', 'A_ITEM',     '품목 관리',    '/m/admin/items',       '마스터',   20, 'Y'),
  ('M_AVEND', 'A_VENDOR',   '매입처 관리',  '/m/admin/vendors',     '마스터',   21, 'Y'),
  ('M_AWH',   'A_WH',       '창고 관리',    '/m/admin/warehouses',  '마스터',   22, 'Y'),
  ('M_AUSER', 'A_USER',     '회원 관리',    '/m/admin/users',       '마스터',   23, 'Y'),
  ('M_APROC', 'A_PROC',     '매입 발주',    '/m/admin/procurements','매입',     30, 'Y'),
  ('M_AINB',  'A_INBOUND',  '입고 처리',    '/m/admin/inbounds',    '매입',     31, 'Y'),
  ('M_AINV',  'A_INV',      '재고 관리',    '/m/admin/inventory',   '매입',     32, 'Y'),
  ('M_AORD',  'A_ORD',      '출고 관리',    '/m/admin/orders',      '출고/정산', 40, 'Y'),
  ('M_APAY',  'A_PAY',      '입금 관리',    '/m/admin/payments',    '출고/정산', 41, 'Y'),
  ('M_AINVO', 'A_INVOICE',  '계산서 발행',  '/m/admin/invoices',    '출고/정산', 42, 'Y'),
  ('M_ASTAT', 'A_STAT_M',   '월간 매출',    '/m/admin/statistics',          '통계', 50, 'Y'),
  ('M_ASTAD', 'A_STAT_D',   '일자별',       '/m/admin/statistics/daily',    '통계', 51, 'Y'),
  ('M_ASTAR', 'A_STAT_R',   '원가/마진',    '/m/admin/statistics/margin',   '통계', 52, 'Y'),
  ('M_AROLE', 'A_ROLE',     '권한 관리',    '/m/admin/roles',       '시스템',   90, 'Y'),
  ('M_AMENU', 'A_MENU',     '메뉴 관리',    '/m/admin/menus',       '시스템',   91, 'Y')
ON CONFLICT (menu_code) DO NOTHING;

-- USER 권한에 거래처 메뉴 매핑
INSERT INTO momo_role_menus (role_code, menu_code, can_read, can_write) VALUES
  ('USER',  'DASHBOARD',  'Y','N'),
  ('USER',  'ITEMS',      'Y','N'),
  ('USER',  'ORDER_NEW',  'Y','Y'),
  ('USER',  'ORDER_LIST', 'Y','N')
ON CONFLICT DO NOTHING;

-- ADMIN 권한에 모든 메뉴 매핑
INSERT INTO momo_role_menus (role_code, menu_code, can_read, can_write)
  SELECT 'ADMIN', menu_code, 'Y', 'Y' FROM momo_menus
ON CONFLICT DO NOTHING;

COMMIT;
