CREATE TABLE IF NOT EXISTS workspace_document_library_items (
  id CHAR(36) NOT NULL,
  law_firm_id CHAR(36) NOT NULL,
  source_code VARCHAR(50) NOT NULL DEFAULT 'manual',
  source_reference VARCHAR(191) NULL,
  document_type_code VARCHAR(50) NOT NULL,
  requirement_code VARCHAR(100) NOT NULL,
  display_name VARCHAR(255) NOT NULL,
  description TEXT NULL,
  ai_instructions TEXT NULL,
  is_required TINYINT(1) NOT NULL DEFAULT 1,
  minimum_quantity INT UNSIGNED NOT NULL DEFAULT 1,
  created_by_user_id CHAR(36) NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_workspace_document_library_source (
    law_firm_id,
    source_code,
    source_reference
  ),
  KEY idx_workspace_document_library_law_firm (law_firm_id, display_name),
  KEY idx_workspace_document_library_document_type (document_type_code),
  CONSTRAINT fk_workspace_document_library_law_firm
    FOREIGN KEY (law_firm_id) REFERENCES law_firms (id),
  CONSTRAINT fk_workspace_document_library_document_type
    FOREIGN KEY (document_type_code) REFERENCES document_types (code),
  CONSTRAINT fk_workspace_document_library_created_by
    FOREIGN KEY (created_by_user_id) REFERENCES users (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
