/**
 * Skill Package Type Definitions.
 * Central types for the skill package data model used across all three creation modes.
 * @module types/skillPackage
 */

import type { SkillCategory } from "@/utils/constants";

/** Represents a single file in a skill package */
export interface SkillPackageFile {
  /** Unique identifier (path-based, e.g., "scripts/deploy.ts") */
  id: string;
  /** File path relative to skill root (e.g., "scripts/deploy.ts" or "SKILL.md") */
  path: string;
  /** Target folder: "root" for SKILL.md, otherwise "scripts" | "references" | "assets" */
  folder: SkillFolder;
  /** Text content (null for binary files) */
  content: string | null;
  /** Original File object for upload (null for generated/virtual files) */
  file: File | null;
  /** File size in bytes */
  size: number;
  /** Whether this file was generated by LLM */
  generated: boolean;
}

/** Valid skill folders */
export type SkillFolder = "root" | "scripts" | "references" | "assets";

/** Uploadable skill folders (excludes "root" since SKILL.md is auto-generated) */
export const UPLOADABLE_FOLDERS = ["scripts", "references", "assets"] as const;
export type UploadableFolder = (typeof UPLOADABLE_FOLDERS)[number];

/** Output type for runtime-based and mixed skills */
export type SkillOutputType = "text" | "file";

/** Nested metadata sub-object matching the frontmatter schema */
export interface SkillMetadataBlock {
  category: SkillCategory;
  outputType?: SkillOutputType;
  runtime: string[];
  runtimeDependency: string[];
  runtimeEnvVar: string[];
  toolList: string[];
  tag: string[];
}

/** Metadata extracted from SKILL.md frontmatter or form fields */
export interface SkillMetadata {
  name: string;
  description: string;
  /**
   * Two-digit version string, e.g. `"0.1"`. Required by the backend on every
   * publish. Guided / generative creation paths default to `"0.1"`.
   */
  version: string;

  /** Nested metadata block (new canonical structure) */
  metadata: SkillMetadataBlock;

  /** Ornn platform extensions */
  license: string;
  compatibility: string;

  /** Official Claude fields */
  disableModelInvocation: boolean;
  userInvocable: boolean;
  allowedTools: string[];
  model: string;
  context: string[];
  agent: string;
  argumentHint: string;
  hooks: Record<string, unknown> | null;
}

/** Creates a default SkillMetadata with sensible defaults */
export function createDefaultSkillMetadata(
  overrides: Partial<SkillMetadata> = {},
): SkillMetadata {
  return {
    name: "",
    description: "",
    version: "0.1",
    metadata: {
      category: "plain",
      outputType: undefined,
      runtime: [],
      runtimeDependency: [],
      runtimeEnvVar: [],
      toolList: [],
      tag: [],
    },
    license: "",
    compatibility: "",
    disableModelInvocation: false,
    userInvocable: true,
    allowedTools: [],
    model: "",
    context: [],
    agent: "",
    argumentHint: "",
    hooks: null,
    ...overrides,
  };
}

/** State for the generative mode conversation */
export interface GenerationConversationMessage {
  role: "user" | "assistant";
  content: string;
}

/** Phase state machine for the generative mode */
export type GenerationPhase =
  | "input"
  | "generating"
  | "preview"
  | "refining"
  | "saving"
  | "error";
