import { createApiRef } from '@backstage/core-plugin-api';

export interface DeploymentSpec {
  name: string;
  namespace: string;
  source: {
    type: 'git' | 'image';
    repository?: string;
    branch?: string;
    image?: string;
    tag?: string;
  };
  resources: {
    cpu: string;
    memory: string;
  };
  environment: Record<string, string>;
  domain?: string;
  backup?: {
    enabled: boolean;
    schedule?: string;
  };
  cluster?: string;
}

export interface Deployment {
  metadata: {
    name: string;
    namespace: string;
    creationTimestamp: string;
  };
  spec: DeploymentSpec;
  status: {
    phase: 'Pending' | 'Running' | 'Failed' | 'Succeeded';
    conditions: Array<{
      type: string;
      status: string;
      reason?: string;
      message?: string;
    }>;
    ready: boolean;
    replicas?: number;
    readyReplicas?: number;
  };
}

export interface ContainerMomApi {
  getDeployments(namespace?: string): Promise<Deployment[]>;
  getDeployment(namespace: string, name: string): Promise<Deployment>;
  createDeployment(spec: DeploymentSpec): Promise<Deployment>;
  updateDeployment(namespace: string, name: string, spec: DeploymentSpec): Promise<Deployment>;
  deleteDeployment(namespace: string, name: string): Promise<void>;
  getLogs(namespace: string, name: string, container?: string): Promise<string[]>;
  getTemplates(): Promise<Template[]>;
}

export interface Template {
  name: string;
  description: string;
  parameters: TemplateParameter[];
  spec: Partial<DeploymentSpec>;
}

export interface TemplateParameter {
  name: string;
  type: 'string' | 'number' | 'boolean' | 'select';
  description: string;
  required: boolean;
  default?: any;
  options?: string[];
}

export const containerMomApiRef = createApiRef<ContainerMomApi>({
  id: 'plugin.deployments.api',
});