// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache-2.0
import type {
  MeterValueDto,
  TenantDto,
  TransactionDto,
  TransactionEventDto,
  TransactionEventEnumType,
  TransactionType,
  TriggerReasonEnumType,
} from '@citrineos/base';
import { DEFAULT_TENANT_ID, OCPP2_0_1, OCPP2_Namespace } from '@citrineos/base';
import {
  BeforeCreate,
  BeforeUpdate,
  BelongsTo,
  Column,
  DataType,
  ForeignKey,
  HasMany,
  Model,
  Table,
} from 'sequelize-typescript';

import { EvseType } from '../DeviceModel/EvseType.js';
import { Tenant } from '../Tenant.js';
import { MeterValue } from './MeterValue.js';
import { Transaction } from './Transaction.js';

@Table
export class TransactionEvent extends Model implements TransactionEventDto {
  static readonly MODEL_NAME: string = OCPP2_Namespace.TransactionEventRequest;

  @Column(DataType.STRING)
  declare ocppConnectionName: string;

  @Column(DataType.STRING)
  declare eventType: TransactionEventEnumType;

  @HasMany(() => MeterValue, 'transactionEventId')
  declare meterValue?: [MeterValueDto, ...MeterValueDto[]];

  @Column({
    type: DataType.DATE,
    get() {
      return this.getDataValue('timestamp')?.toISOString();
    },
  })
  declare timestamp: string;

  @Column(DataType.STRING)
  declare triggerReason: TriggerReasonEnumType;

  @Column(DataType.INTEGER)
  declare seqNo: number;

  @Column({ type: DataType.BOOLEAN, defaultValue: false })
  declare offline?: boolean | null;

  @Column(DataType.INTEGER)
  declare numberOfPhasesUsed?: number | null;

  @Column(DataType.DECIMAL)
  declare cableMaxCurrent?: number | null;

  @Column(DataType.INTEGER)
  declare reservationId?: number | null;

  @ForeignKey(() => Transaction)
  declare transactionDatabaseId?: number;

  @BelongsTo(() => Transaction, 'transactionDatabaseId')
  declare transaction?: TransactionDto;

  @Column(DataType.JSON)
  declare transactionInfo: TransactionType;

  @ForeignKey(() => EvseType)
  declare evseId?: number | null;

  @BelongsTo(() => EvseType, 'evseId')
  declare evse?: EvseType;

  @Column(DataType.STRING)
  declare idTokenValue?: string | null;

  @Column(DataType.STRING)
  declare idTokenType?: string | null;

  declare customData?: OCPP2_0_1.CustomDataType | null;

  @ForeignKey(() => Tenant)
  @Column({
    type: DataType.INTEGER,
    allowNull: false,
    onUpdate: 'CASCADE',
    onDelete: 'RESTRICT',
  })
  declare tenantId: number;

  @BelongsTo(() => Tenant, 'tenantId')
  declare tenant?: TenantDto;

  @BeforeUpdate
  @BeforeCreate
  static setDefaultTenant(instance: TransactionEvent) {
    if (instance.tenantId == null) {
      instance.tenantId = DEFAULT_TENANT_ID;
    }
  }

  constructor(...args: any[]) {
    super(...args);
    if (this.tenantId == null) {
      this.tenantId = DEFAULT_TENANT_ID;
    }
  }
}
