import { PMO_TABLES } from '../pmo/schema.js';
export class MondayMapper {
    db;
    constructor(db) {
        this.db = db;
        this.ensureTable();
    }
    ensureTable() {
        this.db.exec(`
      CREATE TABLE IF NOT EXISTS ${PMO_TABLES.monday_item_map} (
        pmo_ticket_id TEXT NOT NULL REFERENCES ${PMO_TABLES.tickets}(id) ON DELETE CASCADE,
        monday_board_id TEXT NOT NULL,
        monday_item_id TEXT NOT NULL,
        monday_item_name TEXT NOT NULL,
        monday_item_url TEXT,
        sync_direction TEXT NOT NULL DEFAULT 'outbound',
        last_synced_at TIMESTAMP,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (pmo_ticket_id),
        UNIQUE (monday_item_id)
      )
    `);
        this.db.exec(`
      CREATE INDEX IF NOT EXISTS idx_pmo_monday_item_map_item_id
      ON ${PMO_TABLES.monday_item_map}(monday_item_id)
    `);
        this.db.exec(`
      CREATE INDEX IF NOT EXISTS idx_pmo_monday_item_map_board_id
      ON ${PMO_TABLES.monday_item_map}(monday_board_id)
    `);
    }
    createOrUpdateMapping(map) {
        this.db.prepare(`
      INSERT INTO ${PMO_TABLES.monday_item_map}
        (pmo_ticket_id, monday_board_id, monday_item_id, monday_item_name, monday_item_url, sync_direction, last_synced_at, created_at)
      VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
      ON CONFLICT(pmo_ticket_id) DO UPDATE SET
        monday_board_id = excluded.monday_board_id,
        monday_item_id = excluded.monday_item_id,
        monday_item_name = excluded.monday_item_name,
        monday_item_url = excluded.monday_item_url,
        sync_direction = excluded.sync_direction,
        last_synced_at = CURRENT_TIMESTAMP
    `).run(map.pmoTicketId, map.mondayBoardId, map.mondayItemId, map.mondayItemName, map.mondayItemUrl ?? null, map.syncDirection);
    }
    getByTicketId(ticketId) {
        const row = this.db.prepare(`
      SELECT * FROM ${PMO_TABLES.monday_item_map} WHERE pmo_ticket_id = ?
    `).get(ticketId);
        return row ? this.rowToMap(row) : null;
    }
    getByMondayItemId(itemId) {
        const row = this.db.prepare(`
      SELECT * FROM ${PMO_TABLES.monday_item_map} WHERE monday_item_id = ?
    `).get(itemId);
        return row ? this.rowToMap(row) : null;
    }
    listMappings() {
        const rows = this.db.prepare(`
      SELECT * FROM ${PMO_TABLES.monday_item_map} ORDER BY created_at DESC
    `).all();
        return rows.map((row) => this.rowToMap(row));
    }
    updateSyncTimestamp(ticketId) {
        this.db.prepare(`
      UPDATE ${PMO_TABLES.monday_item_map}
      SET last_synced_at = CURRENT_TIMESTAMP
      WHERE pmo_ticket_id = ?
    `).run(ticketId);
    }
    deleteMapping(ticketId) {
        this.db.prepare(`
      DELETE FROM ${PMO_TABLES.monday_item_map}
      WHERE pmo_ticket_id = ?
    `).run(ticketId);
    }
    rowToMap(row) {
        return {
            pmoTicketId: row.pmo_ticket_id,
            mondayBoardId: row.monday_board_id,
            mondayItemId: row.monday_item_id,
            mondayItemName: row.monday_item_name,
            mondayItemUrl: row.monday_item_url ?? undefined,
            syncDirection: row.sync_direction,
            lastSyncedAt: row.last_synced_at ? new Date(row.last_synced_at) : undefined,
            createdAt: new Date(row.created_at),
        };
    }
}
//# sourceMappingURL=mapper.js.map