/**
 * Copyright IBM Corp. 2024, 2025
 */
import { APICFileInfo } from '../../apic-mode/models/apic-file-info.model.js';
import { Metadata } from '../../common/models/base-asset.model.js';
import { VCSFileInfo } from '../../vcs/models/vcs-file-info.model.js';

interface DependentAsset {
  [key: string]: {
    ref: string;
    data: string;
    fileHandle: FileSystemFileHandle | VCSFileInfo | APICFileInfo;
    project: string;
  };
}
export interface PolicySeqAction {
  /**
   * Updates active policy sequence for the form view
   * @param fileHandle Policy sequence file handle
   * @param projectName Name of the project where the policy sequence file resides
   * @returns
   */
  setActivePolicySeq: (
    fileHandle: FileSystemFileHandle | VCSFileInfo | APICFileInfo,
    projectName: string,
  ) => Promise<void>;

  /**
   * Updates active policy for the active policy sequence
   * @param fileHandle Policy file handle
   * @param projectName Name of the project where the policy file resides
   * @returns
   */
  setActivePolicy: (
    fileHandle: FileSystemFileHandle | VCSFileInfo | APICFileInfo,
    projectName: string,
  ) => Promise<void>;

  /**
   * sets active stage of the active policy sequence
   * @param stage Stage of policy sequence
   * @returns
   */
  setActiveStage: (stage: string) => void;

  /**
   * Adds a new policy to the active stage of active policy sequence,
   *  - a new file is not created through this operation
   * @param metadata metadata of the new policy file
   * @param fileHandle filehandle of the new policy file
   * @returns
   */
  addNewPolicy: (
    metadata: Metadata,
    fileHandle: FileSystemFileHandle | VCSFileInfo | APICFileInfo,
  ) => Promise<void>;

  /**
   * Adds an existing policy to the active stage of active policy sequence
   * @param metadata metadata of the existing policy file
   * @returns
   */
  addExistingPolicy: (metadata: Metadata) => Promise<void>;

  /**
   * Removes an existing policy from the active stage of active policy sequence
   * @param metadata metadata of the existing policy file
   * @returns
   */
  removePolicy: (metadata: Metadata) => Promise<void>;

  /**
   * Updates the list of policies of active stage with the list of policies passed in as parameter
   * @param policies List of metadata of policies of the active stage
   * @returns
   */
  updateStagePolicies: (policies: Metadata[]) => Promise<void>;

  /**
   * Updates policy file content with the object given as parameter
   * @param policy Policy Object
   * @param oldMetaData Old metadata if metadata is updated
   * @param newMetaData New metadata if metadata is updated
   * @returns
   */
  updatePolicy: <T>(
    policy: T,
    oldMetaData?: Metadata,
    newMetaData?: Metadata,
  ) => Promise<void>;

  /**
   * Updates asset file content with the object given as parameter
   * @param asset Asset object
   * @param key Dependent asset key
   * @returns
   */
  updateDependentAsset: <T>(asset: T, key: string) => Promise<DependentAsset>;

  /**
   * Sets active dependent asset of the active policy
   * @param fileHandle Asset file handle
   * @param key unique key for the asset
   * @param projectName name of the project where the dependent asset resides in
   * @returns
   */
  setActiveDependentAsset: (
    fileHandle: FileSystemFileHandle | VCSFileInfo | APICFileInfo,
    projectName: string,
    key: string,
  ) => Promise<DependentAsset>;
  /**
   * Removes dependent asset from store
   * @returns
   */
  clearDependentAssets: () => void;
  /**
   * Empties store
   * @returns
   */
  resetStore: () => void;

  /**
   * Stores the given policy sequence content in the zustand store. If content is not given, the active policy sequence data is read and stored
   * @param policySequence The policy sequence content
   * @returns
   */
  updatePolicySequenceContent: (policySequence?: any) => Promise<void>;
}

export interface PolicyAction {
  /**
   * Updates active policy for the active policy sequence
   * @param fileHandle Policy file handle
   * @param projectName Name of the project where the policy file resides
   * @returns
   */
  setActivePolicy: (
    fileHandle: FileSystemFileHandle | VCSFileInfo | APICFileInfo,
    projectName: string,
  ) => Promise<void>;

  /**
   * Updates policy file content with the object given as parameter
   * @param policy Policy Object
   * @returns
   */
  updatePolicy: <T>(policy: T) => Promise<void>;

  /**
   * Updates asset file content with the object given as parameter
   * @param asset Asset object
   * @param key Dependent asset key
   * @returns
   */
  updateDependentAsset: <T>(asset: T, key: string) => Promise<DependentAsset>;

  /**
   * Sets active dependent asset of the active policy
   * @param fileHandle Asset file handle
   * @param key unique key for the asset
   * @param projectName name of the project where the dependent asset resides in
   * @returns
   */
  setActiveDependentAsset: (
    fileHandle: FileSystemFileHandle | VCSFileInfo | APICFileInfo,
    projectName: string,
    key: string,
  ) => Promise<DependentAsset>;
  /**
   * Removes dependent asset from store
   * @returns
   */
  clearDependentAssets: () => void;
  /**
   * Empties store
   * @returns
   */
  resetStore: () => void;
}
