import type { IDiscoveryCandidate, IIntegrationEntity } from '../../core/types.js';

export type TVelbusGatewayConnection = 'serial' | 'tcp' | 'manual';

export type TVelbusChannelKind =
  | 'relay'
  | 'dimmer'
  | 'button'
  | 'led'
  | 'binary_sensor'
  | 'blind'
  | 'sensor'
  | 'temperature'
  | 'counter'
  | 'light_sensor'
  | 'climate';

export type TVelbusHvacMode = 'heat' | 'cool';

export type TVelbusPresetMode = 'away' | 'comfort' | 'eco' | 'home' | 'safe' | 'night' | 'day';

export type TVelbusChannelState = string | number | boolean | null;

export interface IVelbusGatewayConfig {
  id?: string;
  name?: string;
  connection: TVelbusGatewayConnection;
  dsn?: string;
  serialPath?: string;
  host?: string;
  port?: number;
  tls?: boolean;
  password?: string;
  vlpFile?: string;
  manufacturer?: string;
  model?: string;
  serialNumber?: string;
  metadata?: Record<string, unknown>;
}

export interface IVelbusChannel {
  id: string;
  name?: string;
  kind: TVelbusChannelKind;
  channelNumber?: number;
  moduleAddress?: number;
  subDevice?: boolean;
  connected?: boolean;
  state?: TVelbusChannelState;
  brightness?: number;
  position?: number;
  currentTemperature?: number;
  targetTemperature?: number;
  hvacMode?: TVelbusHvacMode;
  presetMode?: TVelbusPresetMode;
  unit?: string;
  deviceClass?: string;
  stateClass?: string;
  counterValue?: number;
  counterUnit?: string;
  options?: string[];
  selectedOption?: string;
  writable?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IVelbusModule {
  address: number;
  id?: string;
  name?: string;
  type?: number | string;
  typeName?: string;
  swVersion?: string;
  serialNumber?: string;
  manufacturer?: string;
  room?: string;
  connected?: boolean;
  channels: IVelbusChannel[];
  attributes?: Record<string, unknown>;
}

export interface IVelbusSnapshot {
  gateway: IVelbusGatewayConfig;
  modules: IVelbusModule[];
  connected: boolean;
  updatedAt?: string;
  events?: IVelbusEvent[];
  limitations?: string[];
}

export interface IVelbusConfig {
  gateway: IVelbusGatewayConfig;
  snapshot?: IVelbusSnapshot;
  mutationMode?: 'manual-snapshot' | 'readonly';
}

export interface IHomeAssistantVelbusConfig extends IVelbusConfig {}

export interface IVelbusEntity extends IIntegrationEntity {
  attributes?: Record<string, unknown> & {
    moduleAddress?: number;
    channelId?: string;
    channelNumber?: number;
    kind?: TVelbusChannelKind;
  };
}

export type TVelbusCommandType = 'turn_on' | 'turn_off' | 'set_value' | 'open' | 'close' | 'stop';

export interface IVelbusCommand {
  type: TVelbusCommandType;
  moduleAddress?: number;
  channelId?: string;
  channelNumber?: number;
  platform?: IIntegrationEntity['platform'];
  entityId?: string;
  deviceId?: string;
  value?: number | string | boolean;
  transition?: number;
  metadata?: Record<string, unknown>;
}

export interface IVelbusCommandResult {
  success: boolean;
  error?: string;
  data?: unknown;
}

export interface IVelbusEvent {
  type: 'state_changed' | 'command_applied' | 'command_failed' | 'gateway_connected' | 'gateway_disconnected';
  moduleAddress?: number;
  channelId?: string;
  entityId?: string;
  deviceId?: string;
  command?: IVelbusCommand;
  data?: unknown;
  timestamp: number;
}

export interface IVelbusManualDiscoveryEntry {
  connection?: TVelbusGatewayConnection;
  dsn?: string;
  serialPath?: string;
  device?: string;
  path?: string;
  host?: string;
  port?: number;
  tls?: boolean;
  password?: string;
  vlpFile?: string;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  serialNumber?: string;
  metadata?: Record<string, unknown>;
}

export interface IVelbusUsbDiscoveryEntry {
  vid?: string;
  pid?: string;
  device?: string;
  serialNumber?: string;
  manufacturer?: string;
  product?: string;
  metadata?: Record<string, unknown>;
}

export interface IVelbusDiscoveryCandidate extends IDiscoveryCandidate {
  metadata?: Record<string, unknown> & {
    connection?: TVelbusGatewayConnection;
    dsn?: string;
    serialPath?: string;
    tls?: boolean;
    password?: string;
    vlpFile?: string;
  };
}
