-- 1. 관리자정보
CREATE TABLE userinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  id VARCHAR(20) NOT NULL UNIQUE,
  passwd VARCHAR(255) NOT NULL,
  name VARCHAR(20) NOT NULL,
  email VARCHAR(50) NOT NULL UNIQUE,
  mobile VARCHAR(20),
  role ENUM('admin', 'manager') DEFAULT 'manager',
  status ENUM('use', 'stop') DEFAULT 'use',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) COMMENT '관리자정보';

-- 2. 고객정보
CREATE TABLE clientinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  phone VARCHAR(50),
  email VARCHAR(100),
  account_balance INT DEFAULT 0 COMMENT '보증금',
  role ENUM('user', 'partner') DEFAULT 'user',
  note TEXT,
  status ENUM('use', 'stop') DEFAULT 'use',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) COMMENT '고객정보';

-- 3. 고객 입출금
CREATE TABLE accountinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  clientinfo_uid INT NOT NULL,
  status ENUM('deposit', 'withdrawal') DEFAULT 'deposit',
  title VARCHAR(255) NOT NULL,
  alias VARCHAR(50) NOT NULL COMMENT '입출금자명',
  amount INT NOT NULL DEFAULT 0,
  note TEXT,
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT FK_accountinfo_TO_clientinfo FOREIGN KEY (clientinfo_uid) REFERENCES clientinfo(uid)
) COMMENT '입출금계좌';

-- 4. 장비 정보
CREATE TABLE deviceinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  type ENUM('server', 'vpc', 'kcs', 'network') DEFAULT 'server',
  brand VARCHAR(50) DEFAULT 'HP',
  model VARCHAR(50) NOT NULL,
  cost_price INT DEFAULT 0,
  price INT DEFAULT 0,
  description TEXT,
  status ENUM('use', 'stop') DEFAULT 'use',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) COMMENT '장비정보';

-- 5. 장비 부속품 정보
CREATE TABLE devicepartinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  type ENUM('memory', 'hdd', 'ssd', 'nic') DEFAULT 'memory',
  brand VARCHAR(50) DEFAULT 'samsung',
  capacity INT NOT NULL COMMENT '용량 (GB)',
  cost_price INT DEFAULT 0,
  price INT DEFAULT 0,
  description TEXT,
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) COMMENT '장비 부품 정보';

-- 6. 장비 부속품 연결
CREATE TABLE deviceinfos_accessories (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  deviceinfo_uid INT NOT NULL,
  devicepartinfo_uid INT NOT NULL,
  softwareinfo_uid INT,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (deviceinfo_uid) REFERENCES deviceinfo(uid),
  FOREIGN KEY (devicepartinfo_uid) REFERENCES devicepartinfo(uid),
  FOREIGN KEY (softwareinfo_uid) REFERENCES softwareinfo(uid)
) COMMENT '장비-부속품 연결정보';

-- 7. 소프트웨어 정보
CREATE TABLE softwareinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  type ENUM('os', 'application') DEFAULT 'os',
  title VARCHAR(100) NOT NULL,
  cost_price INT DEFAULT 0,
  price INT DEFAULT 0,
  description TEXT,
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) COMMENT '소프트웨어 정보';

-- 8. 회선 정보
CREATE TABLE lineinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  code VARCHAR(50) NOT NULL,
  type ENUM('general', 'dedicated') DEFAULT 'general',
  price INT DEFAULT 0,
  status ENUM('use', 'stop') DEFAULT 'use',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) COMMENT '회선 정보';

-- 9. IP 정보
CREATE TABLE ipinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  lineinfo_uid INT NOT NULL,
  deviceinfo_uid INT,
  serviceinfo_uid INT,
  ip_address VARCHAR(50) NOT NULL UNIQUE,
  price INT DEFAULT 0,
  status ENUM('use', 'stop') DEFAULT 'use',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (lineinfo_uid) REFERENCES lineinfo(uid),
  FOREIGN KEY (deviceinfo_uid) REFERENCES deviceinfo(uid),
  FOREIGN KEY (serviceinfo_uid) REFERENCES serviceinfo(uid)
) COMMENT 'IP 정보';

-- 10. 상면 정보 (Rack Space)
CREATE TABLE rackspaceinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  type ENUM('1u', '2u', '4u', 'fullrack', 'lightweight') DEFAULT '1u',
  price INT DEFAULT 0,
  status ENUM('use', 'stop') DEFAULT 'use',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) COMMENT '랙 상면 정보';

-- 11. 서비스 정보
CREATE TABLE serviceinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  clientinfo_uid INT NOT NULL,
  type ENUM('hosting', 'colocation', 'defense') DEFAULT 'hosting',
  title VARCHAR(100) NOT NULL,
  payment_date DATE NOT NULL,
  amount INT DEFAULT 0,
  startdate_at TIMESTAMP NULL,
  enddate_at TIMESTAMP NULL,
  status ENUM('use', 'stop', 'terminate') DEFAULT 'use',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (clientinfo_uid) REFERENCES clientinfo(uid)
) COMMENT '서비스 정보';

-- 12. 청구서 정보
CREATE TABLE invoiceinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  serviceinfo_uid INT NOT NULL,
  type ENUM('monthly', 'onetime', 'daily') DEFAULT 'monthly',
  billing_amount INT DEFAULT 0,
  description TEXT,
  status ENUM('unpaid', 'paid', 'refunded') DEFAULT 'unpaid',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (serviceinfo_uid) REFERENCES serviceinfo(uid)
) COMMENT '청구서 정보';

-- 13. 서비스 - 장비 연결
CREATE TABLE serviceinfos_deviceinfos (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  serviceinfo_uid INT NOT NULL,
  deviceinfo_uid INT NOT NULL,
  payment_type ENUM('onetime', 'month', 'free') DEFAULT 'month',
  amount INT DEFAULT 0,
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (serviceinfo_uid) REFERENCES serviceinfo(uid),
  FOREIGN KEY (deviceinfo_uid) REFERENCES deviceinfo(uid)
) COMMENT '서비스-장비 연결';

-- 14. 서비스 - 장비 부품 연결
CREATE TABLE serviceinfos_devicepartinfos (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  serviceinfo_uid INT NOT NULL,
  devicepartinfo_uid INT NOT NULL,
  payment_type ENUM('onetime', 'month', 'free') DEFAULT 'month',
  amount INT DEFAULT 0,
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (serviceinfo_uid) REFERENCES serviceinfo(uid),
  FOREIGN KEY (devicepartinfo_uid) REFERENCES devicepartinfo(uid)
) COMMENT '서비스-장비 부품 연결';

-- 15. 서비스 - 소프트웨어 연결
CREATE TABLE serviceinfos_softwareinfos (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  serviceinfo_uid INT NOT NULL,
  softwareinfo_uid INT NOT NULL,
  payment_type ENUM('onetime', 'month', 'free') DEFAULT 'month',
  amount INT DEFAULT 0,
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (serviceinfo_uid) REFERENCES serviceinfo(uid),
  FOREIGN KEY (softwareinfo_uid) REFERENCES softwareinfo(uid)
) COMMENT '서비스-소프트웨어 연결';

-- 16. 서비스 - IP 연결
CREATE TABLE serviceinfos_ipinfos (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  serviceinfo_uid INT NOT NULL,
  ipinfo_uid INT NOT NULL,
  payment_type ENUM('onetime', 'month', 'free') DEFAULT 'month',
  amount INT DEFAULT 0,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (serviceinfo_uid) REFERENCES serviceinfo(uid),
  FOREIGN KEY (ipinfo_uid) REFERENCES ipinfo(uid)
) COMMENT '서비스-IP 연결';

-- 17. 서비스 - 회선 연결
CREATE TABLE serviceinfos_lineinfos (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  serviceinfo_uid INT NOT NULL,
  lineinfo_uid INT NOT NULL,
  payment_type ENUM('onetime', 'month', 'free') DEFAULT 'month',
  amount INT DEFAULT 0,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (serviceinfo_uid) REFERENCES serviceinfo(uid),
  FOREIGN KEY (lineinfo_uid) REFERENCES lineinfo(uid)
) COMMENT '서비스-회선 연결';

-- 18. 서비스 - 랙 상면 연결
CREATE TABLE serviceinfos_rackinfos (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  serviceinfo_uid INT NOT NULL,
  rackspaceinfo_uid INT NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (serviceinfo_uid) REFERENCES serviceinfo(uid),
  FOREIGN KEY (rackspaceinfo_uid) REFERENCES rackspaceinfo(uid)
) COMMENT '서비스-랙 연결';

-- 19. 고객 이벤트 (포인트, 도메인 등)
CREATE TABLE eventinfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  clientinfo_uid INT NOT NULL,
  type ENUM('domain', 'point') DEFAULT 'point',
  title VARCHAR(100) NOT NULL,
  value INT NOT NULL,
  note TEXT,
  status ENUM('use', 'expired') DEFAULT 'use',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (clientinfo_uid) REFERENCES clientinfo(uid)
) COMMENT '고객 이벤트 (포인트/도메인)';

-- 20. 작업 로그
CREATE TABLE loginfo (
  uid INT AUTO_INCREMENT PRIMARY KEY,
  userinfo_uid INT NOT NULL,
  type ENUM('info', 'warn', 'error', 'debug') DEFAULT 'info',
  class VARCHAR(255),
  method VARCHAR(255),
  title VARCHAR(255) NOT NULL,
  description TEXT NOT NULL,
  status ENUM('use', 'archived') DEFAULT 'use',
  updated_at TIMESTAMP NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (userinfo_uid) REFERENCES userinfo(uid)
) COMMENT '작업 기록 로그';
