import type * as servezoneInterfaces from '@serve.zone/interfaces';
import * as plugins from '../plugins.ts';
import * as data from '../data/index.ts';

export type IAppStoreApp = servezoneInterfaces.appstore.IAppStoreApp;
export type IAppStoreVersionConfig = servezoneInterfaces.appstore.IAppStoreVersionConfig;
export type IAppStoreAppMeta = servezoneInterfaces.appstore.IAppStoreAppMeta;
export type IUpgradeableAppStoreService = servezoneInterfaces.appstore.IUpgradeableAppStoreService;

export interface IAppStoreInstallOptions extends servezoneInterfaces.appstore.IAppStoreInstallRequest {
  autoDNS?: boolean;
}

export type TAppStoreUpgradeStatus = 'running' | 'success' | 'failed';

export type TAppStoreUpgradeStep =
  | 'queued'
  | 'validating'
  | 'migration'
  | 'applying'
  | 'stopping'
  | 'pulling-image'
  | 'updating-record'
  | 'removing-container'
  | 'creating-container'
  | 'starting'
  | 'restoring-route'
  | 'syncing-gateway'
  | 'complete'
  | 'failed';

export interface IAppStoreUpgradeOperation {
  id: string;
  serviceName: string;
  appTemplateId: string;
  fromVersion: string;
  targetVersion: string;
  status: TAppStoreUpgradeStatus;
  step: TAppStoreUpgradeStep;
  progressLines: string[];
  warnings: string[];
  error?: string;
  startedAt: number;
  updatedAt: number;
  completedAt?: number;
  service?: data.IService;
}

export interface IReq_GetAppStoreTemplates extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetAppStoreTemplates
> {
  method: 'getAppStoreTemplates';
  request: {
    identity: data.IIdentity;
  };
  response: {
    apps: IAppStoreApp[];
  };
}

export interface IReq_GetAppStoreConfig extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetAppStoreConfig
> {
  method: 'getAppStoreConfig';
  request: {
    identity: data.IIdentity;
    appId: string;
    version: string;
  };
  response: {
    config: IAppStoreVersionConfig;
    appMeta: IAppStoreAppMeta;
  };
}

export interface IReq_InstallAppStoreApp extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_InstallAppStoreApp
> {
  method: 'installAppStoreApp';
  request: {
    identity: data.IIdentity;
    install: IAppStoreInstallOptions;
  };
  response: {
    service: data.IService;
  };
}

export interface IReq_GetUpgradeableAppStoreServices extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetUpgradeableAppStoreServices
> {
  method: 'getUpgradeableAppStoreServices';
  request: {
    identity: data.IIdentity;
  };
  response: {
    services: IUpgradeableAppStoreService[];
  };
}

export interface IReq_UpgradeAppStoreService extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_UpgradeAppStoreService
> {
  method: 'upgradeAppStoreService';
  request: {
    identity: data.IIdentity;
    serviceName: string;
    targetVersion: string;
  };
  response: {
    service: data.IService;
    warnings: string[];
  };
}

export interface IReq_StartAppStoreServiceUpgrade extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_StartAppStoreServiceUpgrade
> {
  method: 'startAppStoreServiceUpgrade';
  request: {
    identity: data.IIdentity;
    serviceName: string;
    targetVersion: string;
  };
  response: {
    operation: IAppStoreUpgradeOperation;
  };
}

export interface IReq_GetAppStoreUpgradeOperations extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetAppStoreUpgradeOperations
> {
  method: 'getAppStoreUpgradeOperations';
  request: {
    identity: data.IIdentity;
  };
  response: {
    operations: IAppStoreUpgradeOperation[];
  };
}

export interface IReq_PushAppStoreUpgradeProgress extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_PushAppStoreUpgradeProgress
> {
  method: 'pushAppStoreUpgradeProgress';
  request: {
    operation: IAppStoreUpgradeOperation;
  };
  response: {};
}
