CREATE DATABASE IF NOT EXISTS mzh_library
  DEFAULT CHARACTER SET utf8mb4
  DEFAULT COLLATE utf8mb4_unicode_ci;

USE mzh_library;

CREATE TABLE IF NOT EXISTS roles (
  code VARCHAR(32) PRIMARY KEY,
  name VARCHAR(64) NOT NULL,
  description VARCHAR(255) NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS permissions (
  code VARCHAR(64) PRIMARY KEY,
  name VARCHAR(96) NOT NULL,
  description VARCHAR(255) NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS role_permissions (
  role_code VARCHAR(32) NOT NULL,
  permission_code VARCHAR(64) NOT NULL,
  PRIMARY KEY (role_code, permission_code),
  CONSTRAINT fk_role_permissions_role
    FOREIGN KEY (role_code) REFERENCES roles (code),
  CONSTRAINT fk_role_permissions_permission
    FOREIGN KEY (permission_code) REFERENCES permissions (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS users (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(64) NOT NULL,
  password_hash VARCHAR(255) NOT NULL,
  display_name VARCHAR(100) NOT NULL,
  role_code VARCHAR(32) NOT NULL,
  active TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_users_username (username),
  KEY idx_users_role_code (role_code),
  CONSTRAINT fk_users_role
    FOREIGN KEY (role_code) REFERENCES roles (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS system_logs (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  operator_id BIGINT NULL,
  operator_role VARCHAR(32) NULL,
  operation_type VARCHAR(64) NOT NULL,
  target_table VARCHAR(64) NULL,
  target_id VARCHAR(64) NULL,
  result_status VARCHAR(32) NOT NULL,
  message VARCHAR(500) NULL,
  request_ip VARCHAR(64) NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_system_logs_operator_id (operator_id),
  KEY idx_system_logs_operation_type (operation_type),
  KEY idx_system_logs_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS readers (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  reader_identifier VARCHAR(64) NOT NULL,
  user_id BIGINT NULL,
  full_name VARCHAR(100) NOT NULL,
  phone VARCHAR(32) NULL,
  email VARCHAR(120) NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'active',
  max_borrow_count INT NOT NULL DEFAULT 5,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_readers_identifier (reader_identifier),
  UNIQUE KEY uk_readers_user_id (user_id),
  KEY idx_readers_full_name (full_name),
  KEY idx_readers_phone (phone),
  KEY idx_readers_email (email),
  KEY idx_readers_status (status),
  CONSTRAINT fk_readers_user
    FOREIGN KEY (user_id) REFERENCES users (id),
  CONSTRAINT chk_readers_status
    CHECK (status IN ('active', 'suspended', 'inactive')),
  CONSTRAINT chk_readers_max_borrow_count
    CHECK (max_borrow_count BETWEEN 1 AND 50)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS book_categories (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(96) NOT NULL,
  description VARCHAR(255) NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_book_categories_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS books (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  book_identifier VARCHAR(64) NOT NULL,
  title VARCHAR(200) NOT NULL,
  author VARCHAR(120) NOT NULL,
  category_id BIGINT NOT NULL,
  total_copies INT NOT NULL DEFAULT 0,
  available_copies INT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL DEFAULT 'available',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uk_books_identifier (book_identifier),
  KEY idx_books_title (title),
  KEY idx_books_author (author),
  KEY idx_books_category_id (category_id),
  KEY idx_books_status (status),
  CONSTRAINT fk_books_category
    FOREIGN KEY (category_id) REFERENCES book_categories (id),
  CONSTRAINT chk_books_total_copies
    CHECK (total_copies >= 0),
  CONSTRAINT chk_books_available_copies
    CHECK (available_copies >= 0 AND available_copies <= total_copies),
  CONSTRAINT chk_books_status
    CHECK (status IN ('available', 'unavailable', 'archived'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS borrow_records (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  reader_id BIGINT NOT NULL,
  book_id BIGINT NOT NULL,
  borrowed_at DATETIME NOT NULL,
  due_at DATETIME NOT NULL,
  returned_at DATETIME NULL,
  renewal_count INT NOT NULL DEFAULT 0,
  status VARCHAR(32) NOT NULL DEFAULT 'active',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_borrow_records_reader_id (reader_id),
  KEY idx_borrow_records_book_id (book_id),
  KEY idx_borrow_records_status (status),
  KEY idx_borrow_records_due_at (due_at),
  CONSTRAINT fk_borrow_records_reader
    FOREIGN KEY (reader_id) REFERENCES readers (id),
  CONSTRAINT fk_borrow_records_book
    FOREIGN KEY (book_id) REFERENCES books (id),
  CONSTRAINT chk_borrow_records_renewal_count
    CHECK (renewal_count >= 0),
  CONSTRAINT chk_borrow_records_status
    CHECK (status IN ('active', 'returned'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO roles (code, name, description) VALUES
  ('administrator', 'Administrator', 'Full system administration role'),
  ('librarian', 'Librarian', 'Library operation and borrowing management role'),
  ('reader', 'Reader', 'Reader self-service role')
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  description = VALUES(description);

INSERT INTO permissions (code, name, description) VALUES
  ('manage_users', 'Manage users', 'Create and maintain administrator, librarian, and reader accounts'),
  ('manage_books', 'Manage books', 'Create, update, delete, and categorize books'),
  ('manage_readers', 'Manage readers', 'Maintain reader profiles and eligibility'),
  ('manage_borrowing', 'Manage borrowing', 'Borrow, return, renew, and process overdue records'),
  ('view_reports', 'View reports', 'View search, ranking, inventory, and overdue reports'),
  ('view_system_logs', 'View system logs', 'View system maintenance and exception logs'),
  ('view_catalog', 'View catalog', 'Search and view library books'),
  ('borrow_books', 'Borrow books', 'Borrow and renew own books')
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  description = VALUES(description);

INSERT IGNORE INTO role_permissions (role_code, permission_code) VALUES
  ('administrator', 'manage_users'),
  ('administrator', 'manage_books'),
  ('administrator', 'manage_readers'),
  ('administrator', 'manage_borrowing'),
  ('administrator', 'view_reports'),
  ('administrator', 'view_system_logs'),
  ('administrator', 'view_catalog'),
  ('librarian', 'manage_books'),
  ('librarian', 'manage_readers'),
  ('librarian', 'manage_borrowing'),
  ('librarian', 'view_reports'),
  ('librarian', 'view_catalog'),
  ('reader', 'view_catalog'),
  ('reader', 'borrow_books');

-- Demo accounts for local scaffold verification only:
--   admin/admin123, librarian/librarian123, reader/reader123.
-- Change or remove them before using a non-local database.
INSERT IGNORE INTO users (username, password_hash, display_name, role_code, active) VALUES
  ('admin', 'pbkdf2_sha256$60000$Ren1B30RDysysnApRiFVaQ==$1XwzMHaALqC7dKffwjbQkilBedfAuiMOXbR/xTMr5+Y=', 'System Administrator', 'administrator', 1),
  ('librarian', 'pbkdf2_sha256$60000$PV/DJwZlMRm8vy0lKMAM4g==$+Aijfop3YoPp6HTePN5r4wG8N3qgxJE+yZHkTfzfbaw=', 'Library Staff', 'librarian', 1),
  ('reader', 'pbkdf2_sha256$60000$wBzxTIT4ep79hgEzYDV9aQ==$w3oO5iSKRSfG4++b4558yiTHy6Tz9BB2+wuV9UOAKhs=', 'Demo Reader', 'reader', 1);

INSERT IGNORE INTO readers (reader_identifier, user_id, full_name, phone, email, status, max_borrow_count) VALUES
  ('RD-0001', (SELECT id FROM users WHERE username = 'reader'), 'Demo Reader', '13800000000',
    'reader@example.com', 'active', 5),
  ('RD-0002', NULL, 'Suspended Reader', '13900000000', 'suspended.reader@example.com', 'suspended', 3),
  ('RD-0101', NULL, '张晓雨', '13600010001', 'zhang.xiaoyu@example.com', 'active', 6),
  ('RD-0102', NULL, '李明远', '13600010002', 'li.mingyuan@example.com', 'active', 5),
  ('RD-0103', NULL, '王思涵', '13600010003', 'wang.sihan@example.com', 'active', 4),
  ('RD-0104', NULL, '赵晨', '13600010004', 'zhao.chen@example.com', 'suspended', 3);

INSERT INTO book_categories (name, description) VALUES
  ('Computer Science', 'Programming, software engineering, and systems books'),
  ('Literature', 'Classic and modern literature'),
  ('History', 'World and regional history'),
  ('Science', 'Natural science and popular science'),
  ('中国文学', '中国现当代文学、经典小说和散文作品'),
  ('计算机技术', '程序设计、软件工程、数据库和信息技术图书'),
  ('历史文化', '中国历史、世界历史和文化研究读物'),
  ('自然科学', '数学、物理、生命科学和科普读物'),
  ('社会科学', '社会学、管理学和公共事务读物')
ON DUPLICATE KEY UPDATE
  description = VALUES(description);

INSERT IGNORE INTO books (book_identifier, title, author, category_id, total_copies, available_copies, status) VALUES
  ('BK-0001', 'Effective Java', 'Joshua Bloch',
    (SELECT id FROM book_categories WHERE name = 'Computer Science'), 5, 4, 'available'),
  ('BK-0002', 'Clean Code', 'Robert C. Martin',
    (SELECT id FROM book_categories WHERE name = 'Computer Science'), 4, 2, 'available'),
  ('BK-0003', 'Pride and Prejudice', 'Jane Austen',
    (SELECT id FROM book_categories WHERE name = 'Literature'), 3, 3, 'available'),
  ('BK-0004', 'A Brief History of Time', 'Stephen Hawking',
    (SELECT id FROM book_categories WHERE name = 'Science'), 2, 1, 'available'),
  ('BK-0101', '活着', '余华',
    (SELECT id FROM book_categories WHERE name = '中国文学'), 6, 5, 'available'),
  ('BK-0102', '平凡的世界', '路遥',
    (SELECT id FROM book_categories WHERE name = '中国文学'), 5, 5, 'available'),
  ('BK-0103', '深入理解Java虚拟机', '周志明',
    (SELECT id FROM book_categories WHERE name = '计算机技术'), 4, 3, 'available'),
  ('BK-0104', '中国通史', '吕思勉',
    (SELECT id FROM book_categories WHERE name = '历史文化'), 3, 3, 'available'),
  ('BK-0105', '乡土中国', '费孝通',
    (SELECT id FROM book_categories WHERE name = '社会科学'), 4, 4, 'available'),
  ('BK-0106', '科学史十五讲', '江晓原',
    (SELECT id FROM book_categories WHERE name = '自然科学'), 3, 2, 'available');
