import { BaseEntity } from "./base";
import { TimeEntryDto, TimeEntryCreateDto, TimeEntryUpdateDto } from "./dto";
import { UserId, ProjectId, ActivityId, TimeEntryId } from "./value-ids";

export type TimeEntry = Omit<TimeEntryDto, "id" | "start" | "end" | "userId" | "projectId" | "activityId" | "lastEditorID" | "createdAt" | "updatedAt"> & {
    id: TimeEntryId;
    start: Date;
    end: Date;
    userId: UserId;
    projectId: ProjectId;
    activityId: ActivityId;
} & BaseEntity;

export const mapTimeEntryDtoToTimeEntry = (dto: TimeEntryDto): TimeEntry => ({
    ...dto,
    id: dto.id as TimeEntryId,
    start: new Date(dto.start),
    end: new Date(dto.end),
    userId: dto.userId.toString() as UserId,
    projectId: dto.projectId.toString() as ProjectId,
    activityId: dto.activityId.toString() as ActivityId,
    createdAt: new Date(dto.createdAt),
    updatedAt: new Date(dto.updatedAt),
    lastEditorID: dto.lastEditorID as UserId,
});

export type TimeEntryCreate = Omit<TimeEntryCreateDto, "start" | "end"> & {
    start: Date;
    end: Date;
};

export const mapTimeEntryCreateDtoToTimeEntryCreate = (dto: TimeEntryCreateDto): TimeEntryCreate => ({
    ...dto,
    start: new Date(dto.start),
    end: new Date(dto.end),
});

export type TimeEntryUpdate = Omit<TimeEntryUpdateDto, "start" | "end"> & {
    start?: Date;
    end?: Date;
};

export const mapTimeEntryUpdateDtoToTimeEntryUpdate = (dto: TimeEntryUpdateDto): TimeEntryUpdate => ({
    ...dto,
    start: dto.start ? new Date(dto.start) : undefined,
    end: dto.end ? new Date(dto.end) : undefined,
});
