import { z } from 'zod';
import type { ToolDef, ToolPack } from '../../tool-registry.js';

const UserRole = z.enum(['SUPER_ADMIN', 'INFLUENCE_ADMIN', 'MAP_ADMIN', 'USER', 'TEMP']);

const adminTools: ToolDef[] = [
  {
    name: 'users_create',
    description:
      'Create a new platform user. Assign a role: SUPER_ADMIN (full access), ' +
      'INFLUENCE_ADMIN (campaigns), MAP_ADMIN (map/canvassing), USER (volunteer), TEMP (limited).',
    inputSchema: {
      email: z.string().email().describe('User email address'),
      password: z.string().min(12).describe('Password (12+ chars, upper, lower, digit required)'),
      name: z.string().optional().describe('Display name'),
      role: UserRole.default('USER').describe('User role'),
    },
    method: 'POST',
    path: '/api/users',
    tier: 2,
  },
  {
    name: 'users_update',
    description: 'Update a user: change role, name, email, or status.',
    inputSchema: {
      id: z.string().describe('User ID'),
      email: z.string().email().optional().describe('Email address'),
      name: z.string().optional().describe('Display name'),
      role: UserRole.optional().describe('User role'),
    },
    method: 'PUT',
    path: '/api/users/:id',
    tier: 2,
  },
  {
    name: 'users_delete',
    description: 'Delete a user account. This is permanent.',
    inputSchema: {
      id: z.string().describe('User ID'),
    },
    method: 'DELETE',
    path: '/api/users/:id',
    tier: 2,
  },
  {
    name: 'settings_update',
    description:
      'Update site settings: organization name, feature flags (enableMedia, enableSms, ' +
      'enableEvents, enablePayments), SMTP config, etc.',
    inputSchema: {
      organizationName: z.string().max(200).optional().describe('Organization name'),
      domain: z.string().max(200).optional().describe('Primary domain'),
      enableMedia: z.boolean().optional().describe('Enable media/video features'),
      enableSms: z.boolean().optional().describe('Enable SMS features'),
      enableEvents: z.boolean().optional().describe('Enable Gancio event integration'),
      enablePayments: z.boolean().optional().describe('Enable payment/donation features'),
    },
    method: 'PUT',
    path: '/api/settings',
    tier: 2,
  },
  {
    name: 'observability_metrics',
    description:
      'Get platform observability metrics from Prometheus: API request rates, ' +
      'latency percentiles, error rates, active sessions.',
    inputSchema: {},
    method: 'GET',
    path: '/api/observability/metrics',
    tier: 2,
  },
  {
    name: 'observability_alerts',
    description: 'Get active Alertmanager alerts for the platform.',
    inputSchema: {},
    method: 'GET',
    path: '/api/observability/alerts',
    tier: 2,
  },
  {
    name: 'pangolin_status',
    description:
      'Get Pangolin tunnel status: connection state, configured services, ' +
      'and Newt container health.',
    inputSchema: {},
    method: 'GET',
    path: '/api/pangolin/status',
    tier: 2,
  },
  {
    name: 'docker_containers',
    description:
      'Get status of all Docker containers in the platform stack. ' +
      'Returns container name, status, uptime, and resource usage.',
    inputSchema: {},
    method: 'GET',
    path: '/api/dashboard/containers',
    tier: 2,
  },
  {
    name: 'system_info',
    description: 'Get system hardware info: CPU, memory, disk, OS version.',
    inputSchema: {},
    method: 'GET',
    path: '/api/dashboard/system',
    tier: 2,
  },
  {
    name: 'locations_create',
    description:
      'Create a new map location. Provide an address and it will be auto-geocoded.',
    inputSchema: {
      address: z.string().min(1).max(500).describe('Full street address'),
      unit: z.string().max(50).optional().describe('Unit/apartment number'),
      city: z.string().max(200).optional().describe('City'),
      province: z.string().max(100).optional().describe('Province/state'),
      postalCode: z.string().max(20).optional().describe('Postal/ZIP code'),
      notes: z.string().max(2000).optional().describe('Notes about this location'),
    },
    method: 'POST',
    path: '/api/map/locations',
    tier: 2,
  },
  {
    name: 'registry_status',
    description:
      'List all Docker images stored in the Gitea container registry. ' +
      'Shows image names, available tags (commit SHAs + latest), and upload dates. ' +
      'Use to verify which image versions are available for deployment.',
    inputSchema: {},
    method: 'GET',
    path: '/api/registry/status',
    tier: 2,
  },
  {
    name: 'registry_build_push',
    description:
      'Trigger a production Docker image build and push to the Gitea registry. ' +
      'Builds api, admin, media-api, nginx (and optionally code-server), tags with ' +
      'current commit SHA + latest, then pushes to the configured registry. ' +
      'Runs in background — use registry_status to confirm completion.',
    inputSchema: {
      skipCodeServer: z.boolean().optional()
        .describe('Skip code-server image (large ~10GB, slow push). Default: omit to include all'),
      services: z.string().optional()
        .describe('Comma-separated services to build, e.g. "api,admin,nginx". Default: all'),
    },
    method: 'POST',
    path: '/api/registry/build-push',
    tier: 2,
  },
  {
    name: 'registry_mirror',
    description:
      'Mirror essential third-party Docker images (postgres, redis, nocodb, listmonk, ' +
      'prometheus, grafana, etc.) from Docker Hub to the Gitea registry. ' +
      'Protects against Docker Hub rate limits. Runs in background.',
    inputSchema: {
      all: z.boolean().optional()
        .describe('Also mirror heavy optional images: Rocket.Chat, MongoDB, n8n, Jitsi (default: false)'),
    },
    method: 'POST',
    path: '/api/registry/mirror',
    tier: 2,
  },
];

export const adminPack: ToolPack = {
  name: 'admin',
  description: 'User CRUD, settings, observability, Pangolin, Docker status, container registry',
  tools: adminTools,
};
