/** Resource types that a pi package can provide. */
export type ResourceType = "extension" | "skill" | "prompt" | "theme";

/** A single resource declared by a package (from the `pi` manifest). */
export interface PiManifestResource {
  path: string;
  type: ResourceType;
}

/** The `pi` field from package.json. */
export interface PiManifest {
  extensions?: string[];
  skills?: string[];
  prompts?: string[];
  themes?: string[];
  video?: string;
  image?: string;
}

/** A package from npm search results. */
export interface NpmSearchResult {
  name: string;
  version: string;
  description: string;
  downloads: number;
  publisher: string;
  links: {
    npm: string;
    homepage?: string;
    repository?: string;
  };
  date: string;
  keywords: string[];
  /** Inferred resource types from the `pi` manifest. */
  resourceTypes: ResourceType[];
}

/** A resource within an installed package (shown in detail view). */
export interface InstalledResource {
  path: string;
  type: ResourceType;
  /** Whether this resource is filtered out (disabled). */
  enabled: boolean;
}

/** A package filter entry from settings.json (can filter which resources load). */
export interface PackageFilterEntry {
  source: string;
  extensions?: string[];
  skills?: string[];
  prompts?: string[];
  themes?: string[];
}

/** An installed package (from ~/.pi/agent/settings.json or .pi/settings.json). */
export interface InstalledPackage {
  /** The raw source string, e.g. "npm:pi-web-access". */
  source: string;
  /** Package name (from npm / package.json). */
  name: string;
  /** Resolved version. */
  version?: string;
  /** Human-readable description. */
  description?: string;
  /** Install scope. */
  scope: "global" | "project";
  /** Local install path. */
  installPath?: string;
  /** Resources this package provides. */
  resources: InstalledResource[];
  /** The raw `pi` manifest from package.json (for display). */
  piManifest?: PiManifest;
  /** Repository URL (clean HTTPS, from package.json). */
  repository?: string;
  /** Homepage URL (from package.json). */
  homepage?: string;
}

/** Response from GET /api/packages. */
export interface ListPackagesResponse {
  packages: InstalledPackage[];
  error?: string;
}

/** Response from the npm registry search endpoint. */
export interface NpmRegistryObject {
  package: {
    name: string;
    version: string;
    description: string;
    keywords?: string[];
    date: string;
    publisher?: { username: string; email?: string };
    links: {
      npm: string;
      homepage?: string;
      repository?: string;
      bugs?: string;
    };
  };
  downloads: {
    monthly: number;
    weekly: number;
  };
  score: {
    final: number;
    detail: { popularity: number; quality: number; maintenance: number };
  };
}

export interface NpmRegistrySearchResponse {
  objects: NpmRegistryObject[];
  total: number;
  time: string;
}
