/**
 * Service-related data shapes for Onebox
 */

import type { IPlatformRequirements } from './platform.js';
import type * as servezoneInterfaces from '@serve.zone/interfaces';

export type TServiceStatus = 'stopped' | 'starting' | 'running' | 'stopping' | 'failed';
export type IServiceTargetPort = servezoneInterfaces.data.IServiceTargetPort;
export type IServiceDomainRoute = servezoneInterfaces.data.IServiceDomainRoute;
export type IServicePublicPortMapping = servezoneInterfaces.data.IServicePublicPortMapping;
export type IServiceSecretFile = servezoneInterfaces.data.IServiceSecretFile;

export interface IService {
  id?: number;
  name: string;
  image: string;
  registry?: string;
  envVars: Record<string, string>;
  containerArgs?: string[];
  secretFiles?: IServiceSecretFile[];
  volumes?: IServiceVolume[];
  publishedPorts?: IServicePublishedPort[];
  targetPorts?: IServiceTargetPort[];
  domains?: IServiceDomainRoute[];
  publicPortMappings?: IServicePublicPortMapping[];
  port: number;
  domain?: string;
  containerID?: string;
  status: TServiceStatus;
  createdAt: number;
  updatedAt: number;
  // Onebox Registry fields
  useOneboxRegistry?: boolean;
  registryRepository?: string;
  registryImageTag?: string;
  autoUpdateOnPush?: boolean;
  imageDigest?: string;
  imageCleanup?: IServiceImageCleanup;
  // Platform service requirements
  platformRequirements?: IPlatformRequirements;
  // Backup settings
  includeImageInBackup?: boolean;
  // App Store template tracking
  appTemplateId?: string;
  appTemplateVersion?: string;
}

export interface IServiceImageCleanup {
  imageName: string;
  candidateReference: string;
  priorReference?: string;
  phase?: 'rollback-pending' | 'candidate-active';
}

export interface IServiceVolume {
  name?: string;
  source?: string;
  mountPath: string;
  storageClass?: string;
  driver?: string;
  readOnly?: boolean;
  backup?: boolean;
  options?: Record<string, string>;
}

export type TServicePortProtocol = 'tcp' | 'udp';

export interface IServicePublishedPort {
  targetPort: number;
  targetPortEnd?: number;
  publishedPort?: number;
  publishedPortEnd?: number;
  protocol?: TServicePortProtocol;
  hostIp?: string;
}

export interface IServiceCreate {
  name: string;
  image: string;
  port: number;
  domain?: string;
  envVars?: Record<string, string>;
  containerArgs?: string[];
  secretFiles?: IServiceSecretFile[];
  volumes?: IServiceVolume[];
  publishedPorts?: IServicePublishedPort[];
  targetPorts?: IServiceTargetPort[];
  domains?: IServiceDomainRoute[];
  publicPortMappings?: IServicePublicPortMapping[];
  useOneboxRegistry?: boolean;
  registryImageTag?: string;
  autoUpdateOnPush?: boolean;
  enableMongoDB?: boolean;
  enableS3?: boolean;
  enableClickHouse?: boolean;
  enableValkey?: boolean;
  enableMariaDB?: boolean;
  appTemplateId?: string;
  appTemplateVersion?: string;
}

export interface IServiceUpdate {
  image?: string;
  registry?: string;
  port?: number;
  domain?: string;
  envVars?: Record<string, string>;
  containerArgs?: string[] | null;
  secretFiles?: IServiceSecretFile[] | null;
  volumes?: IServiceVolume[];
  publishedPorts?: IServicePublishedPort[];
  targetPorts?: IServiceTargetPort[];
  domains?: IServiceDomainRoute[];
  publicPortMappings?: IServicePublicPortMapping[];
  forcePull?: boolean;
}

export interface IContainerStats {
  cpuPercent: number;
  memoryUsed: number;
  memoryLimit: number;
  memoryPercent: number;
  networkRx: number;
  networkTx: number;
}

export interface IMetric {
  id?: number;
  serviceId: number;
  timestamp: number;
  cpuPercent: number;
  memoryUsed: number;
  memoryLimit: number;
  networkRxBytes: number;
  networkTxBytes: number;
}

export interface ILogEntry {
  id?: number;
  serviceId: number;
  timestamp: number;
  message: string;
  level: 'info' | 'warn' | 'error' | 'debug';
  source: 'stdout' | 'stderr';
}
