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

import type { IPlatformRequirements } from './platform.ts';

export type TServiceStatus = 'stopped' | 'starting' | 'running' | 'stopping' | 'failed';

export interface IService {
  id?: number;
  name: string;
  image: string;
  registry?: string;
  envVars: Record<string, string>;
  volumes?: IServiceVolume[];
  publishedPorts?: IServicePublishedPort[];
  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;
  // Platform service requirements
  platformRequirements?: IPlatformRequirements;
  // Backup settings
  includeImageInBackup?: boolean;
  // App Store template tracking
  appTemplateId?: string;
  appTemplateVersion?: string;
}

export interface IServiceVolume {
  name?: string;
  source?: string;
  mountPath: 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>;
  volumes?: IServiceVolume[];
  publishedPorts?: IServicePublishedPort[];
  useOneboxRegistry?: boolean;
  registryImageTag?: string;
  autoUpdateOnPush?: boolean;
  enableMongoDB?: boolean;
  enableS3?: boolean;
  enableClickHouse?: boolean;
  enableRedis?: boolean;
  enableMariaDB?: boolean;
  appTemplateId?: string;
  appTemplateVersion?: string;
}

export interface IServiceUpdate {
  image?: string;
  registry?: string;
  port?: number;
  domain?: string;
  envVars?: Record<string, string>;
  volumes?: IServiceVolume[];
  publishedPorts?: IServicePublishedPort[];
}

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';
}
