import {
    TimeEntry,
    TimeEntryStatus,
    TimeEntryChargeCode,
    Company,
    ServiceTicket,
    Activity,
    Contact,
    CorporateLocation,
} from "../../generated/prisma/client";

type TimeEntryWithRelations = TimeEntry & {
    status?: TimeEntryStatus | null;
    chargeCode?: TimeEntryChargeCode | null;
    company?: Company | null;
    serviceTicket?: ServiceTicket | null;
    activity?: Activity | null;
    contact?: Contact | null;
    location?: CorporateLocation | null;
};

export class TimeEntryController {
    public readonly id: number;
    public readonly uid: string;

    private _data: TimeEntryWithRelations;

    constructor(data: TimeEntryWithRelations) {
        this.id = data.id;
        this.uid = data.uid;
        this._data = data;
    }

    public toJson() {
        const d = this._data;
        return {
            id: d.uid,
            cwId: d.id,
            memberId: d.memberId,

            // ------ Time Fields ------
            dateStart: d.dateStart,
            timeStart: d.timeStart,
            timeEnd: d.timeEnd,

            // ------ Notes ------
            notes: d.notes,
            notesMd: d.notesMd,
            internalNote: d.internalNote,

            // ------ Hours ------
            billableHours: d.billableHours,
            actualHours: d.actualHours,
            invoicedHours: d.invoicedHours,
            deductedHours: d.deductedHours,

            // ------ Rates ------
            hourlyRate: d.hourlyRate,
            effectiveRate: d.effectiveRate,

            // ------ Flag Fields ------
            issueFlag: d.issueFlag,
            mergedFlag: d.mergedFlag,
            invoiceFlag: d.invoiceFlag,
            billableFlag: d.billableFlag,
            documentFlag: d.documentFlag,
            teProblemFlag: d.teProblemFlag,
            teResolutionFlag: d.teResolutionFlag,
            teInternalAnalysisFlag: d.teInternalAnalysisFlag,

            // ------ Charge Info ------
            chargeToRecId: d.chargeToRecId,
            chargeToType: d.chargeToType,

            // ------ Relations ------
            company: d.company
                ? { id: d.company.uid, cwId: d.company.id, name: d.company.name }
                : null,
            serviceTicket: d.serviceTicket
                ? {
                    id: d.serviceTicket.uid,
                    cwId: d.serviceTicket.id,
                    summary: d.serviceTicket.summary,
                }
                : null,
            activity: d.activity
                ? {
                    id: d.activity.uid,
                    cwId: d.activity.id,
                    subject: d.activity.subject,
                }
                : null,
            contact: d.contact
                ? {
                    id: d.contact.uid,
                    cwId: d.contact.id,
                    name: `${d.contact.firstName} ${d.contact.lastName}`.trim(),
                }
                : null,
            location: d.location
                ? { id: d.location.uid, cwId: d.location.id, name: d.location.name }
                : null,
            status: d.status
                ? {
                    id: d.status.uid,
                    cwId: d.status.id,
                    description: d.status.description,
                    action: d.status.action,
                }
                : null,
            chargeCode: d.chargeCode
                ? {
                    id: d.chargeCode.uid,
                    cwId: d.chargeCode.id,
                    description: d.chargeCode.description,
                    expenseFlag: d.chargeCode.expenseFlag,
                    timeFlag: d.chargeCode.timeFlag,
                    billableFlag: d.chargeCode.billableFlag,
                    invoiceFlag: d.chargeCode.invoiceFlag,
                }
                : null,

            // ------ Audit ------
            createdById: d.createdById,
            updatedById: d.updatedById,
            originalAuthorId: d.originalAuthorId,
            createdAt: d.createdAt,
            updatedAt: d.updatedAt,
        };
    }
}
