import { BaseController, LoggerPort } from '@humand-packages/common';
import { NextFunction, Request } from 'express';
import { OK } from 'http-status-codes';
import { DeprecatedHumandResponse } from 'humand-packages/monolith/src/api/types/locals';
import { Service } from 'typedi';
import { PermissionsService } from '../../../../permissions/business/services/permissionsService';
import { User } from '../../../../users/business/models/user';
import { UserDAO } from '../../../../users/infrastructure/dataAccessObjects/userDAO';
import { userDAOtoUser } from '../../../../users/infrastructure/mappers/usersMapper';
import TimeTrackingUserSettingService from '../../../business/services/timeTracking/timeTrackingUserSetting';
import { TimeTrackingKioskPinBulkUpsertVC } from '../../validationClasses/timeTracking/timeTrackingKioskPinBulkUpsertVC';
import { TimeTrackingUserSettingUpdateKioskPinVC } from '../../validationClasses/timeTracking/timeTrackingKioskPinUpdateVC';

@Service()
export class TimeTrackingUserSettingController extends BaseController {
    constructor(
        private readonly permissionService: PermissionsService,
        private readonly timeTrackingUserSettingService: TimeTrackingUserSettingService,
        logger: LoggerPort,
    ) {
        super(logger);
    }

    public getByUserId = async (_req: Request, res: DeprecatedHumandResponse, next: NextFunction): Promise<void> => {
        const { loggedUser, userId } = res.locals;
        const user = userDAOtoUser(loggedUser as UserDAO);
        await this.endpointHandlerWithGenerics(next, async () => {
            this.permissionService.validateManageTimeTracking(user);
            const userSetting = await this.timeTrackingUserSettingService.getTimeTrackingUserSetting((user as User).instanceId, userId);
            res.json(userSetting);
            res.status(OK).end();
        });
    };

    public updateKioskPin = async (_req: Request, res: DeprecatedHumandResponse, next: NextFunction): Promise<void> => {
        const { loggedUser, userId } = res.locals;
        const user = userDAOtoUser(loggedUser as UserDAO);
        const params = res.locals.bodyParams as TimeTrackingUserSettingUpdateKioskPinVC;
        await this.endpointHandlerWithGenerics(next, async () => {
            this.permissionService.validateManageTimeTracking(user);
            const updated = await this.timeTrackingUserSettingService.updateKioskPin((user as User).instanceId, userId, params.kioskPin);
            res.json(updated);
            res.status(OK).end();
        });
    };

    public bulkUpsert = async (_req: Request, res: DeprecatedHumandResponse, next: NextFunction): Promise<void> => {
        const { loggedUser } = res.locals;
        const user = userDAOtoUser(loggedUser as UserDAO);
        const params = res.locals.bodyParams as TimeTrackingKioskPinBulkUpsertVC;
        await this.endpointHandlerWithGenerics(next, async () => {
            this.permissionService.validateManageTimeTracking(user);
            const result = await this.timeTrackingUserSettingService.bulkUpsertKioskPin((user as User).instanceId, params);
            res.json(result);
            res.status(OK).end();
        });
    };
}
