-- Phase 129-01: extend order_notes o pola dla notatek autorskich (user-authored)
-- Reuse istniejacej tabeli z nowym note_type='user', user_id (FK->users), author_name (snapshot).
-- Idempotentna: guard przez information_schema; no-op po pierwszym uruchomieniu.
-- Pattern z Key Decision 2026-05-10: migracje no-op zawsze jako DDL (ALTER TABLE COMMENT),
-- nigdy SELECT 1 (PDO unbuffered + result set -> SQLSTATE 2014).

-- 1) ADD COLUMN user_id
SET @col_user_id := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'order_notes'
    AND COLUMN_NAME = 'user_id'
);
SET @sql_user_id := IF(@col_user_id = 0,
  'ALTER TABLE order_notes ADD COLUMN user_id INT UNSIGNED NULL AFTER note_type',
  'ALTER TABLE order_notes COMMENT = ''phase-129 user_id no-op'''
);
PREPARE s1 FROM @sql_user_id; EXECUTE s1; DEALLOCATE PREPARE s1;

-- 2) ADD COLUMN author_name
SET @col_author_name := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'order_notes'
    AND COLUMN_NAME = 'author_name'
);
SET @sql_author_name := IF(@col_author_name = 0,
  'ALTER TABLE order_notes ADD COLUMN author_name VARCHAR(190) NULL AFTER user_id',
  'ALTER TABLE order_notes COMMENT = ''phase-129 author_name no-op'''
);
PREPARE s2 FROM @sql_author_name; EXECUTE s2; DEALLOCATE PREPARE s2;

-- 3) ADD FOREIGN KEY user_id -> users(id) ON DELETE SET NULL
SET @fk_exists := (
  SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'order_notes'
    AND CONSTRAINT_NAME = 'order_notes_user_fk'
);
SET @sql_fk := IF(@fk_exists = 0,
  'ALTER TABLE order_notes ADD CONSTRAINT order_notes_user_fk FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE',
  'ALTER TABLE order_notes COMMENT = ''phase-129 fk no-op'''
);
PREPARE s3 FROM @sql_fk; EXECUTE s3; DEALLOCATE PREPARE s3;

-- 4) ADD INDEX (note_type, order_id) — wspiera subquery user_notes_count i listUserNotes
SET @idx_exists := (
  SELECT COUNT(*) FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'order_notes'
    AND INDEX_NAME = 'idx_order_notes_type_order'
);
SET @sql_idx := IF(@idx_exists = 0,
  'ALTER TABLE order_notes ADD INDEX idx_order_notes_type_order (note_type, order_id)',
  'ALTER TABLE order_notes COMMENT = ''phase-129 idx no-op'''
);
PREPARE s4 FROM @sql_idx; EXECUTE s4; DEALLOCATE PREPARE s4;
