// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache-2.0

import type { BootstrapConfig } from '@citrineos/base';
import type { IChangeConfigurationRepository } from '../../../interfaces/repositories.js';
import { ChangeConfiguration } from '../model/ChangeConfiguration.js';
import { SequelizeRepository } from './Base.js';
import type { ILogObj } from 'tslog';
import { Logger } from 'tslog';
import { Sequelize } from 'sequelize-typescript';

export class SequelizeChangeConfigurationRepository
  extends SequelizeRepository<ChangeConfiguration>
  implements IChangeConfigurationRepository
{
  constructor(config: BootstrapConfig, logger?: Logger<ILogObj>, sequelizeInstance?: Sequelize) {
    super(config, ChangeConfiguration.MODEL_NAME, logger, sequelizeInstance);
  }

  async createOrUpdateChangeConfiguration(
    tenantId: number,
    configuration: ChangeConfiguration,
  ): Promise<ChangeConfiguration | undefined> {
    let changeConfiguration: ChangeConfiguration | undefined;
    await this.s.transaction(async (sequelizeTransaction) => {
      const [savedConfig, created] = await this.readOrCreateByQuery(tenantId, {
        where: {
          tenantId: tenantId,
          ocppConnectionName: configuration.ocppConnectionName,
          key: configuration.key,
        },
        defaults: {
          ...configuration,
        },
        transaction: sequelizeTransaction,
      });
      if (created) {
        changeConfiguration = savedConfig;
      } else {
        changeConfiguration = await savedConfig.update(
          { ...configuration },
          { transaction: sequelizeTransaction },
        );
        this.emit('updated', [changeConfiguration]);
      }
    });

    return changeConfiguration;
  }
}
