import { AgentSchema } from './agent.ts';
import { CreateProjectRequestSchema, NewProjectSchema } from './create.ts';
import {
	CreateProjectDeploymentSchema,
	DeploymentCompleteSchema,
	DeploymentConfig,
	DeploymentFailPayloadSchema,
	DeploymentStatusSchema,
} from './deploy.ts';
import { ProjectDeleteRequestSchema } from './delete.ts';
import { ProjectEnvDeleteRequestSchema } from './env-delete.ts';
import { ProjectEnvUpdateRequestSchema } from './env-update.ts';
import { ProjectHostnameSetRequestSchema } from './hostname.ts';
import { MalwareCheckResultSchema } from './malware.ts';
import {
	HostnameGetResponseDataSchema,
	HostnameSetResponseDataSchema,
	MalwareCheckRequestSchema,
} from './types.ts';
import { UpdateRegionRequestSchema } from './update-region.ts';
import type { Service } from '../api-reference.ts';

const service: Service = {
	name: 'Projects',
	slug: 'projects',
	description:
		'Full project lifecycle management including deployments, agents, environment variables, and hostnames',
	endpoints: [
		{
			id: 'create-project',
			title: 'Create Project',
			sectionTitle: 'Project Management',
			method: 'POST',
			path: '/cli/project',
			description: 'Create a new project within an organization.',
			pathParams: [],
			queryParams: [],
			requestBody: {
				description: 'Project creation payload.',
				fields: { schema: CreateProjectRequestSchema },
			},
			responseDescription: 'Returns the created project ID and SDK key.',
			responseFields: { schema: NewProjectSchema },
			statuses: [
				{ code: 201, description: 'Project created' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
			],
			examplePath: '/cli/project',
			exampleBody: { name: 'my-project', orgId: 'org_abc123', cloudRegion: 'usw' },
		},
		{
			id: 'list-projects',
			title: 'List Projects',
			sectionTitle: 'Project Management',
			method: 'GET',
			path: '/cli/project',
			description: 'List all projects accessible to the authenticated user.',
			pathParams: [],
			queryParams: [
				{
					name: 'hasDeployment',
					type: 'boolean',
					description: 'Filter to projects with deployments',
					required: false,
				},
				{
					name: 'limit',
					type: 'number',
					description: 'Max results (default 1000, max 10000)',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Returns a list of projects.',
			statuses: [
				{ code: 200, description: 'Projects returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
			],
			examplePath: '/cli/project',
		},
		{
			id: 'get-project',
			title: 'Get Project',
			sectionTitle: 'Project Management',
			method: 'GET',
			path: '/cli/project/{id}',
			description:
				'Retrieve a specific project by ID. The `mask` and `includeProjectKeys` query parameters both default to `true` when omitted.',
			pathParams: [{ name: 'id', type: 'string', description: 'Project ID', required: true }],
			queryParams: [
				{
					name: 'mask',
					type: 'boolean',
					description: 'Mask secrets (default true)',
					required: false,
				},
				{
					name: 'includeProjectKeys',
					type: 'boolean',
					description: 'Include SDK keys (default true)',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Returns the project details.',
			statuses: [
				{ code: 200, description: 'Project returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/project/proj_abc123',
		},
		{
			id: 'delete-projects',
			title: 'Delete Projects',
			sectionTitle: 'Project Management',
			method: 'DELETE',
			path: '/cli/project',
			description: 'Delete one or more projects by ID.',
			pathParams: [],
			queryParams: [],
			requestBody: {
				description: 'Project deletion payload.',
				fields: { schema: ProjectDeleteRequestSchema },
			},
			responseDescription: 'Returns array of deleted project IDs.',
			statuses: [
				{ code: 200, description: 'Projects deleted' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
			],
			examplePath: '/cli/project',
			exampleBody: { ids: ['proj_abc123'] },
		},
		{
			id: 'check-project-exists',
			title: 'Check Project Exists',
			sectionTitle: 'Project Management',
			method: 'GET',
			path: '/cli/project/exists/{name}',
			description: 'Check if a project with the given name already exists.',
			pathParams: [
				{
					name: 'name',
					type: 'string',
					description: 'Project name (URL-encoded)',
					required: true,
				},
			],
			queryParams: [
				{ name: 'orgId', type: 'string', description: 'Organization ID', required: true },
			],
			requestBody: null,
			responseDescription: 'Returns true (HTTP 409) if exists, false (HTTP 422) if not.',
			statuses: [
				{ code: 409, description: 'Project exists' },
				{ code: 422, description: 'Project does not exist' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
			],
			examplePath: '/cli/project/exists/my-project?orgId=org_abc123',
		},
		{
			id: 'update-project-region',
			title: 'Update Project Region',
			sectionTitle: 'Project Management',
			method: 'PATCH',
			path: '/cli/project/{id}',
			description: 'Update the cloud region for a project.',
			pathParams: [{ name: 'id', type: 'string', description: 'Project ID', required: true }],
			queryParams: [],
			requestBody: {
				description: 'Region update payload.',
				fields: { schema: UpdateRegionRequestSchema },
			},
			responseDescription: 'Returns the updated project.',
			statuses: [
				{ code: 200, description: 'Project updated' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/project/proj_abc123',
			exampleBody: { cloudRegion: 'use' },
		},
		{
			id: 'update-env-variables',
			title: 'Update Env Variables',
			sectionTitle: 'Environment Variables',
			method: 'PUT',
			path: '/cli/project/{id}/env',
			description: 'Update environment variables and secrets for a project.',
			pathParams: [{ name: 'id', type: 'string', description: 'Project ID', required: true }],
			queryParams: [],
			requestBody: {
				description: 'Environment variable update payload.',
				fields: { schema: ProjectEnvUpdateRequestSchema, omit: ['id'] },
			},
			responseDescription: 'Returns the updated environment variables.',
			statuses: [
				{ code: 200, description: 'Environment variables updated' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/project/proj_abc123/env',
			exampleBody: { env: { DB_HOST: 'localhost' }, secrets: { DB_PASS: 'secret' } },
		},
		{
			id: 'delete-env-variables',
			title: 'Delete Env Variables',
			sectionTitle: 'Environment Variables',
			method: 'DELETE',
			path: '/cli/project/{id}/env',
			description: 'Delete specific environment variables and secrets from a project.',
			pathParams: [{ name: 'id', type: 'string', description: 'Project ID', required: true }],
			queryParams: [],
			requestBody: {
				description: 'Environment variable deletion payload.',
				fields: { schema: ProjectEnvDeleteRequestSchema, omit: ['id'] },
			},
			responseDescription: 'Empty response on success.',
			statuses: [
				{ code: 204, description: 'Environment variables deleted' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/project/proj_abc123/env',
			exampleBody: { env: ['OLD_VAR'], secrets: ['OLD_SECRET'] },
		},
		{
			id: 'list-agents',
			title: 'List Agents',
			sectionTitle: 'Agents',
			method: 'GET',
			path: '/cli/agent/{projectId}',
			description: 'List agents for a project.',
			pathParams: [
				{ name: 'projectId', type: 'string', description: 'Project ID', required: true },
			],
			queryParams: [
				{
					name: 'deploymentId',
					type: 'string',
					description: 'Filter by deployment ID',
					required: false,
				},
				{
					name: 'orgId',
					type: 'string',
					description: 'Filter by organization ID',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Returns a list of agents for the project.',
			responseFields: { schema: AgentSchema },
			statuses: [
				{ code: 200, description: 'Agents returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/agent/proj_abc123',
		},
		{
			id: 'get-agent-by-identifier',
			title: 'Get Agent by Identifier',
			sectionTitle: 'Agents',
			method: 'GET',
			path: '/cli/agent/{projectId}',
			description: 'Retrieve a specific agent by its identifier.',
			pathParams: [
				{ name: 'projectId', type: 'string', description: 'Project ID', required: true },
			],
			queryParams: [
				{ name: 'identifier', type: 'string', description: 'Agent identifier', required: true },
			],
			requestBody: null,
			responseDescription: 'Returns the agent matching the identifier.',
			statuses: [
				{ code: 200, description: 'Agent returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Agent not found' },
			],
			examplePath: '/cli/agent/proj_abc123?identifier=my-agent',
		},
		{
			id: 'list-deployments',
			title: 'List Deployments',
			sectionTitle: 'Deployments',
			method: 'GET',
			path: '/cli/project/{projectId}/deployments',
			description: 'List deployments for a project.',
			pathParams: [
				{ name: 'projectId', type: 'string', description: 'Project ID', required: true },
			],
			queryParams: [
				{
					name: 'limit',
					type: 'number',
					description: 'Max results (default 10)',
					required: false,
				},
				{
					name: 'orgId',
					type: 'string',
					description: 'Filter by organization ID',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Returns a list of deployments for the project.',
			statuses: [
				{ code: 200, description: 'Deployments returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/project/proj_abc123/deployments',
		},
		{
			id: 'get-deployment',
			title: 'Get Deployment',
			sectionTitle: 'Deployments',
			method: 'GET',
			path: '/cli/project/{projectId}/deployments/{deploymentId}',
			description: 'Retrieve a specific deployment by ID.',
			pathParams: [
				{ name: 'projectId', type: 'string', description: 'Project ID', required: true },
				{ name: 'deploymentId', type: 'string', description: 'Deployment ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Returns the deployment details.',
			statuses: [
				{ code: 200, description: 'Deployment returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Deployment not found' },
			],
			examplePath: '/cli/project/proj_abc123/deployments/dep_def456',
		},
		{
			id: 'delete-deployment',
			title: 'Delete Deployment',
			sectionTitle: 'Deployments',
			method: 'DELETE',
			path: '/cli/project/{projectId}/deployments/{deploymentId}',
			description: 'Delete a specific deployment.',
			pathParams: [
				{ name: 'projectId', type: 'string', description: 'Project ID', required: true },
				{ name: 'deploymentId', type: 'string', description: 'Deployment ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Returns confirmation of deletion.',
			statuses: [
				{ code: 200, description: 'Deployment deleted' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Deployment not found' },
			],
			examplePath: '/cli/project/proj_abc123/deployments/dep_def456',
		},
		{
			id: 'rollback-deployment',
			title: 'Rollback Deployment',
			sectionTitle: 'Deployments',
			method: 'POST',
			path: '/cli/project/{projectId}/deployments/{deploymentId}/rollback',
			description: 'Rollback to a specific deployment version.',
			pathParams: [
				{ name: 'projectId', type: 'string', description: 'Project ID', required: true },
				{ name: 'deploymentId', type: 'string', description: 'Deployment ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Rolls back to the specified deployment version.',
			statuses: [
				{ code: 200, description: 'Rollback initiated' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Deployment not found' },
			],
			examplePath: '/cli/project/proj_abc123/deployments/dep_def456/rollback',
		},
		{
			id: 'undeploy-project',
			title: 'Undeploy Project',
			sectionTitle: 'Deployments',
			method: 'POST',
			path: '/cli/project/{projectId}/deployments/undeploy',
			description: 'Undeploy all active deployments for a project.',
			pathParams: [
				{ name: 'projectId', type: 'string', description: 'Project ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Returns confirmation of undeployment.',
			statuses: [
				{ code: 200, description: 'Project undeployed' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/project/proj_abc123/deployments/undeploy',
		},
		{
			id: 'get-deployment-logs',
			title: 'Get Deployment Logs',
			sectionTitle: 'Deployments',
			method: 'GET',
			path: '/cli/project/{projectId}/deployments/{deploymentId}/logs',
			description: 'Retrieve logs for a specific deployment.',
			pathParams: [
				{ name: 'projectId', type: 'string', description: 'Project ID', required: true },
				{ name: 'deploymentId', type: 'string', description: 'Deployment ID', required: true },
			],
			queryParams: [
				{
					name: 'limit',
					type: 'number',
					description: 'Max results (default 100)',
					required: false,
				},
			],
			requestBody: null,
			responseDescription: 'Returns deployment log entries.',
			statuses: [
				{ code: 200, description: 'Logs returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Deployment not found' },
			],
			examplePath: '/cli/project/proj_abc123/deployments/dep_def456/logs',
		},
		{
			id: 'get-deployment-info',
			title: 'Get Deployment Info',
			sectionTitle: 'Deployments',
			method: 'GET',
			path: '/cli/deployment/{deploymentId}',
			description:
				'Lightweight deployment lookup — returns ID, project, org, region, state, and active status.',
			pathParams: [
				{ name: 'deploymentId', type: 'string', description: 'Deployment ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription:
				'Lightweight deployment lookup — returns ID, project, org, region, state, and active status.',
			statuses: [
				{ code: 200, description: 'Deployment info returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Deployment not found' },
			],
			examplePath: '/cli/deployment/dep_abc123',
		},
		{
			id: 'start-deployment',
			title: 'Start Deployment',
			sectionTitle: 'Deploy Pipeline',
			method: 'POST',
			path: '/cli/deploy/2/start/{projectId}',
			description: 'Start a new deployment for a project.',
			pathParams: [
				{ name: 'projectId', type: 'string', description: 'Project ID', required: true },
			],
			queryParams: [],
			requestBody: {
				description: 'Deployment configuration payload.',
				fields: { schema: DeploymentConfig },
			},
			responseDescription: 'Returns deployment details and upload information.',
			responseFields: { schema: CreateProjectDeploymentSchema, stripRequired: true },
			statuses: [
				{ code: 201, description: 'Deployment started' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/deploy/2/start/proj_abc123',
			exampleBody: { resources: { memory: 512, cpu: 1 } },
		},
		{
			id: 'upload-build-metadata',
			title: 'Upload Build Metadata',
			sectionTitle: 'Deploy Pipeline',
			method: 'PUT',
			path: '/cli/deploy/2/start/{deploymentId}',
			description: 'Upload build metadata for a deployment in progress.',
			pathParams: [
				{ name: 'deploymentId', type: 'string', description: 'Deployment ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription:
				'Returns pre-signed upload URLs for the deployment archive and assets.',
			statuses: [
				{ code: 200, description: 'Upload URLs returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Deployment not found' },
			],
			examplePath: '/cli/deploy/2/start/dep_abc123',
		},
		{
			id: 'complete-deployment',
			title: 'Complete Deployment',
			sectionTitle: 'Deploy Pipeline',
			method: 'POST',
			path: '/cli/deploy/2/complete/{deploymentId}',
			description: 'Signal that a deployment upload is complete and ready for activation.',
			pathParams: [
				{ name: 'deploymentId', type: 'string', description: 'Deployment ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Returns warmup stream and public URL information.',
			responseFields: { schema: DeploymentCompleteSchema },
			statuses: [
				{ code: 200, description: 'Deployment completed' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Deployment not found' },
			],
			examplePath: '/cli/deploy/2/complete/dep_abc123',
		},
		{
			id: 'get-deploy-status',
			title: 'Get Deploy Status',
			sectionTitle: 'Deploy Pipeline',
			method: 'GET',
			path: '/cli/deploy/2/status/{deploymentId}',
			description: 'Check the current status of a deployment.',
			pathParams: [
				{ name: 'deploymentId', type: 'string', description: 'Deployment ID', required: true },
			],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Returns the current deployment state.',
			responseFields: { schema: DeploymentStatusSchema, stripRequired: true },
			statuses: [
				{ code: 200, description: 'Status returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Deployment not found' },
			],
			examplePath: '/cli/deploy/2/status/dep_abc123',
		},
		{
			id: 'report-deploy-failure',
			title: 'Report Deploy Failure',
			sectionTitle: 'Deploy Pipeline',
			method: 'POST',
			path: '/cli/deploy/2/fail/{deploymentId}',
			description: 'Report a deployment failure with error details.',
			pathParams: [
				{ name: 'deploymentId', type: 'string', description: 'Deployment ID', required: true },
			],
			queryParams: [],
			requestBody: {
				description: 'Failure report payload.',
				fields: { schema: DeploymentFailPayloadSchema },
			},
			responseDescription: 'Returns confirmation of failure report.',
			statuses: [
				{ code: 200, description: 'Failure reported' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Deployment not found' },
			],
			examplePath: '/cli/deploy/2/fail/dep_abc123',
			exampleBody: { error: 'Build failed: TypeScript compilation error' },
		},
		{
			id: 'get-hostname',
			title: 'Get Hostname',
			sectionTitle: 'Hostname',
			method: 'GET',
			path: '/cli/project/{id}/hostname',
			description: 'Get the vanity hostname for a project.',
			pathParams: [{ name: 'id', type: 'string', description: 'Project ID', required: true }],
			queryParams: [],
			requestBody: null,
			responseDescription: 'Returns the vanity hostname and URL.',
			responseFields: { schema: HostnameGetResponseDataSchema, stripRequired: true },
			statuses: [
				{ code: 200, description: 'Hostname returned' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/project/proj_abc123/hostname',
		},
		{
			id: 'set-hostname',
			title: 'Set Hostname',
			sectionTitle: 'Hostname',
			method: 'PUT',
			path: '/cli/project/{id}/hostname',
			description: 'Set a vanity hostname for a project.',
			pathParams: [{ name: 'id', type: 'string', description: 'Project ID', required: true }],
			queryParams: [],
			requestBody: {
				description: 'Hostname configuration payload.',
				fields: { schema: ProjectHostnameSetRequestSchema },
			},
			responseDescription: 'Returns the configured hostname and URL.',
			responseFields: { schema: HostnameSetResponseDataSchema, stripRequired: true },
			statuses: [
				{ code: 200, description: 'Hostname set' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
				{ code: 404, description: 'Project not found' },
			],
			examplePath: '/cli/project/proj_abc123/hostname',
			exampleBody: { hostname: 'my-app' },
		},
		{
			id: 'check-malware',
			title: 'Check for Malware',
			sectionTitle: 'Security',
			method: 'POST',
			path: '/security/{deploymentId}/malware-check',
			description: 'Scan deployment dependencies for known malware.',
			pathParams: [
				{
					name: 'deploymentId',
					type: 'string',
					description: 'Deployment ID (URL-encoded)',
					required: true,
				},
			],
			queryParams: [],
			requestBody: {
				description: 'Malware scan payload.',
				fields: { schema: MalwareCheckRequestSchema },
			},
			responseDescription:
				"Returns scan results. If action is 'block', the deployment should be blocked.",
			responseFields: { schema: MalwareCheckResultSchema, stripRequired: true },
			statuses: [
				{ code: 200, description: 'Scan completed' },
				{ code: 401, description: 'Unauthorized — invalid or missing API key' },
			],
			examplePath: '/security/dep_abc123/malware-check',
			exampleBody: { ecosystem: 'npm', packages: [{ name: 'lodash', version: '4.17.21' }] },
		},
	],
};

export default service;
