{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.ai-skills;

  # Derive repo root from skillsPath (skills/ is a subdirectory)
  repoRoot = dirOf cfg.skillsPath;

  skillsData = import ../skills.nix;

  skillsList = ''
    Available skills:
${concatStringsSep "\n" (map (name: "    - ${name}: ${skillsData.${name}}") (attrNames skillsData))}
  '';
in {
  options.services.ai-skills = {
    enable = mkEnableOption "AI agent skills for Claude Code, OpenCode, and Codex";

    skillsPath = mkOption {
      type = types.path;
      default = null;
      description = "Path to skills repository (e.g., ~/proj/skills/skills)";
    };

    # Per-target skill lists
    claudeCodeSkills = mkOption {
      type = types.listOf types.str;
      default = [];
      description = "Skills to deploy to Claude Code (~/.claude/skills/). ${skillsList}";
      example = [ "worklog" "niri-window-capture" ];
    };

    openCodeSkills = mkOption {
      type = types.listOf types.str;
      default = [];
      description = "Skills to deploy to OpenCode (~/.config/opencode/skills/). ${skillsList}";
      example = [ "worklog" "web-search" ];
    };

    codexSkills = mkOption {
      type = types.listOf types.str;
      default = [];
      description = "Skills to deploy to Codex (~/.codex/skills/). ${skillsList}";
      example = [ "worklog" "hq" ];
    };

    # Lenses for orch multi-model review
    enableLenses = mkOption {
      type = types.bool;
      default = true;
      description = "Deploy review lenses to ~/.config/lenses/";
    };

    # Workflows (beads protos)
    enableWorkflows = mkOption {
      type = types.bool;
      default = true;
      description = "Deploy workflow protos to ~/.beads/molecules.jsonl";
    };
  };

  config = mkIf cfg.enable {
    home.file = mkMerge [
      # Claude Code skills
      (mkIf (cfg.claudeCodeSkills != []) (
        builtins.listToAttrs (
          map (skillName: {
            name = ".claude/skills/${skillName}";
            value = {
              source = "${cfg.skillsPath}/${skillName}";
              recursive = true;
            };
          }) cfg.claudeCodeSkills
        )
      ))

      # OpenCode skills
      (mkIf (cfg.openCodeSkills != []) (
        builtins.listToAttrs (
          map (skillName: {
            name = ".config/opencode/skills/${skillName}";
            value = {
              source = "${cfg.skillsPath}/${skillName}";
              recursive = true;
            };
          }) cfg.openCodeSkills
        )
      ))

      # Codex skills
      (mkIf (cfg.codexSkills != []) (
        builtins.listToAttrs (
          map (skillName: {
            name = ".codex/skills/${skillName}";
            value = {
              source = "${cfg.skillsPath}/${skillName}";
              recursive = true;
            };
          }) cfg.codexSkills
        )
      ))

      # Lenses for orch (separate subdirectories per skill)
      (mkIf cfg.enableLenses {
        ".config/lenses/code" = {
          source = "${cfg.skillsPath}/code-review/lenses";
          recursive = true;
        };
        ".config/lenses/ops" = {
          source = "${cfg.skillsPath}/ops-review/lenses";
          recursive = true;
        };
      })

      # Workflows (beads protos)
      (mkIf cfg.enableWorkflows {
        ".beads/molecules.jsonl" = {
          source = "${repoRoot}/workflows/molecules.jsonl";
        };
      })
    ];
  };
}
