/**
 * Change Set Repository Port Interface
 *
 * Port for ChangeSet aggregate operations.
 * Implementations (Drizzle, etc.) are adapters.
 */

import type { ChangeSet, ChangeSetEntry } from '../schema.js';

export type CreateChangeSetData = {
  agentId: string | null;
  agentName: string;
  initiativeId: string;
  mode: 'plan' | 'detail' | 'refine' | 'chat';
  summary?: string | null;
};

export type CreateChangeSetEntryData = {
  entityType: 'page' | 'phase' | 'task' | 'phase_dependency' | 'task_dependency';
  entityId: string;
  action: 'create' | 'update' | 'delete';
  previousState?: string | null;
  newState?: string | null;
  sortOrder?: number;
};

export type ChangeSetWithEntries = ChangeSet & { entries: ChangeSetEntry[] };

export interface ChangeSetRepository {
  createWithEntries(data: CreateChangeSetData, entries: CreateChangeSetEntryData[]): Promise<ChangeSet>;
  findById(id: string): Promise<ChangeSet | null>;
  findByIdWithEntries(id: string): Promise<ChangeSetWithEntries | null>;
  findByInitiativeId(initiativeId: string): Promise<ChangeSet[]>;
  findByAgentId(agentId: string): Promise<ChangeSet[]>;
  markReverted(id: string): Promise<ChangeSet>;

  /**
   * Find applied changesets that have a 'create' entry for the given entity.
   * Used to reconcile changeset status when entities are manually deleted.
   */
  findAppliedByCreatedEntity(entityType: string, entityId: string): Promise<ChangeSetWithEntries[]>;
}
