/**
 * init CLI types — Input spec and output result.
 *
 * `gitflow init <url>` is a DETERMINISTIC, single-shot operation:
 *   bare-clone the repo → import main + develop as worktrees → create the
 *   features/releases/hotfixes folders → write the canonical config.
 * It reproduces the reference layout (e.g. D:/01 - projets/smartstack.license):
 *   <source>/<name>/.git            (bare repo, core.bare=true)
 *   <source>/<name>/01-Main         (worktree: main)        — or main    if !numbered
 *   <source>/<name>/02-Develop      (worktree: develop)     — or develop if !numbered
 *   <source>/<name>/{features,releases,hotfixes}
 *   <source>/<name>/.gitflow/config.json   (shared by all worktrees)
 */

import { z } from 'zod';

export const InitSpecSchema = z.object({
  /** Repository URL to import (https or ssh). Required to actually build. */
  url: z.string().optional(),
  /** Parent folder under which the <name> project root is created (default: cwd). */
  source: z.string().optional(),
  /** Project folder name. Derived from the URL when omitted. */
  name: z.string().optional(),
  /**
   * Explicit project root, decoupling the folder from the project name
   * (e.g. clone FribikeSharing INTO an existing client folder D:/.../TPF).
   * When omitted, root = <source>/<name>.
   */
  root: z.string().optional(),
  /** Reuse a root that already contains a git repo instead of refusing. */
  force: z.boolean().default(false),
  /**
   * THE folder-naming choice the user makes:
   *   true  → numbered  `01-Main` / `02-Develop`  (SmartStack default)
   *   false → plain      `main`     / `develop`
   */
  numbered: z.boolean().default(true),
  /**
   * Worktree layout mode stored in the config. Derived from `numbered` when
   * omitted (numbered → "organized", plain → "simple"); never prompted for.
   */
  mode: z.enum(['organized', 'simple', 'disabled']).optional(),
  /** Override the imported main / develop branch names. */
  mainBranch: z.string().optional(),
  developBranch: z.string().optional(),
  /**
   * Dry proposal mode: derive the name + check the target, return suggestions,
   * touch NOTHING on disk. Lets the skill confirm name/source with the user
   * before the (network) clone runs.
   */
  propose: z.boolean().default(false),
});

export type InitSpec = z.infer<typeof InitSpecSchema>;

export interface InitResult {
  success: boolean;
  /** True when the skill must collect/confirm input (missing url, name proposal). */
  needsInput?: boolean;
  detected?: {
    platform: string;
    provider: string;
    defaultBranch?: string;
  };
  /** Returned in propose mode (and when the target already exists). */
  proposal?: {
    name: string;
    source: string;
    root: string;
    numbered: boolean;
    exists: boolean;
    alternates: string[];
  };
  /** Paths created on a successful build. */
  structure?: {
    root: string;
    bare: string;
    main: string;
    develop: string;
    features: string;
    releases: string;
    hotfixes: string;
    configPath: string;
  };
  /** The canonical GitFlow config that was written. */
  config?: unknown;
  validation?: {
    blockers: string[];
    warnings: string[];
  };
  error?: string;
}
