import { ActivityLogDao } from "../dao/activityLog.dao";
import { LanguageService } from "../services/language.service";
import { Common } from "../../utils/common";
import { AppError } from "../../utils/errors";

export class ActivityLogService {
    private accountId: number | null;
    private userId: number | null;
    private language: string;
    private scope: string[] | null;
    private config: userConfig | null;
    private activityLogDao: ActivityLogDao;
    private languageService: LanguageService;
    constructor(
        private options: {
            language: string;
            scope: string[] | null;
            accountId: number | null;
            userId: number | null;
            config: userConfig | null;
        } = {
                language: process.env.DEFAULT_LANGUAGE_CODE!,
                scope: null,
                config: null,
                userId: null,
                accountId: null,
            }
    ) {
        this.language = options.language;
        this.scope = options.scope ?? [];
        this.config = options.config ?? null;
        this.userId = options.userId ?? null;
        this.accountId = options.accountId ?? null;
        this.activityLogDao = new ActivityLogDao({
            userId: this.userId,
            accountId: this.accountId,
            language: this.language,
            scope: this.scope,
            config: this.config
        });
        this.languageService = new LanguageService({
            userId: this.userId,
            accountId: this.accountId,
            language: this.language,
            scope: this.scope,
            config: this.config
        });
    }

    // 

    // list activities
    getActivityLogs = async (input: ActivityLogGetActivityLogsServiceInput): Promise<ActivityPaginatedList> => {
        try {
            const { listRequest } = input;
            const { page, perPage } = listRequest
            const getActivityLogsDaoInput: ActivityLogGetActivityLogsDaoInput = { listRequest };
            let activities: ActivityLogPaginatedData = await this.activityLogDao.getActivityLogs(getActivityLogsDaoInput, {});
            // Map activities to the desired format
            let activitiesLogs = activities.rows.map(activity => {
                return {
                    id: activity.id,
                    ip: activity.ip,
                    code: activity.activity.code,
                    title: Common.applyReplacements(activity.activity.title, activity.replacements),
                    description: Common.applyReplacements(activity.activity.description, activity.replacements),
                    descriptionText: Common.applyReplacements(activity.activity.descriptionText, activity.replacements),
                    location: activity.location,
                    deviceInfo: activity.deviceInfo,
                    createdAt: activity.createdAt,
                    updatedAt: activity.updatedAt
                };
            });
            let totalPages = Common.getTotalPages(activities.count, perPage);
            return {
                data: activitiesLogs,
                page: page,
                perPage: perPage,
                totalRecords: activities.count,
                totalPages: totalPages
            } as unknown as ActivityPaginatedList;

        } catch (err) {
            if (err instanceof AppError) { throw err; }
            throw new AppError(500, 'SOMETHING_WENT_WRONG_IN_SERVICE', err);
        }
    }
}
