import { ENV_OPTIONAL_NUMBER_KEYS_MAPPING, SEQUELIZE_INSTANCE_TOKEN } from '@humand-packages/common';
import { Sequelize } from 'sequelize-typescript';
import { Inject, Service } from 'typedi';
import { BaseRepositoryAdapter } from '../../../../../dbAdapters/baseRepositoryAdapter';
import { TimeTrackingSetting, TimeTrackingSettingForCreation } from '../../../business/models/timeTrackingSetting';
import { TimeTrackingSettingDAO } from '../../dataAccessObjects/setting';
import { mapSettingDAOToModel, mapSettingModelToDAO } from '../../mappers/timeTrackingSettingMapper';
import TimeTrackingSettingRepositoryPort from '../../ports/TimeTrackingSettingRepositoryPort';

@Service()
export default class TimeTrackingSettingRepositoryAdapter extends BaseRepositoryAdapter implements TimeTrackingSettingRepositoryPort {
    constructor(
        @Inject(ENV_OPTIONAL_NUMBER_KEYS_MAPPING.BULK_CREATE_CHUNK_SIZE) bulkCreateChunkSize: number,
        @Inject(SEQUELIZE_INSTANCE_TOKEN) sequelize: Sequelize,
    ) {
        super(bulkCreateChunkSize, sequelize);
    }

    public getByInstanceId = async (instanceId: number): Promise<TimeTrackingSetting | null> => {
        const enntityDAO = await TimeTrackingSettingDAO.findOne({ where: { instanceId } });
        return enntityDAO ? mapSettingDAOToModel(enntityDAO) : null;
    };

    public create = async (entity: TimeTrackingSettingForCreation): Promise<TimeTrackingSetting> => {
        const entityDAO = mapSettingModelToDAO(entity);
        await entityDAO.save();
        return mapSettingDAOToModel(entityDAO);
    };

    public update = async (updated: TimeTrackingSetting): Promise<TimeTrackingSetting> => {
        const enntityDAO = mapSettingModelToDAO(updated);
        await enntityDAO.save();
        return mapSettingDAOToModel(enntityDAO);
    };
}
