import { db, ObjectType, RowModelRequest, RowRequestSchema, UserProfile } from "@nice/common";
import { RowCacheService } from "../base/row-cache.service";
import { isFieldCondition, LogicalCondition } from "../base/sql-builder";
import { z } from "zod";
export class RoleRowService extends RowCacheService {
    protected createGetRowsFilters(
        request: z.infer<typeof RowRequestSchema>,
        staff?: UserProfile
    ) {
        const condition = super.createGetRowsFilters(request)
        if (isFieldCondition(condition))
            return {}
        const baseModelCondition: LogicalCondition[] = [{
            field: `${this.tableName}.deleted_at`,
            op: "blank",
            type: "date"
        }]
        condition.AND = [...baseModelCondition, ...condition.AND!]
        return condition
    }
    createUnGroupingRowSelect(): string[] {
        return [
            `${this.tableName}.id AS id`,
            `${this.tableName}.name AS name`,
            `${this.tableName}.system AS system`,
            `${this.tableName}.permissions AS permissions`
        ];
    }
    protected async getRowDto(data: any, staff?: UserProfile): Promise<any> {
        if (!data.id)
            return data
        const roleMaps = await db.roleMap.findMany({
            where: {
                roleId: data.id
            }
        })
        const deptIds = roleMaps.filter(item => item.objectType === ObjectType.DEPARTMENT).map(roleMap => roleMap.objectId)
        const staffIds = roleMaps.filter(item => item.objectType === ObjectType.STAFF).map(roleMap => roleMap.objectId)
        const depts = await db.department.findMany({ where: { id: { in: deptIds } } })
        const staffs = await db.staff.findMany({ where: { id: { in: staffIds } } })
        const result = { ...data, depts, staffs }
        return result
    }
    createJoinSql(request?: RowModelRequest): string[] {
        return [];
    }
}