import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { List } from '@iqser/common-ui/lib/utils';
import { DossierStats, IDossier } from '@red/domain';
import { DossierTemplatesService } from '@services/dossier-templates/dossier-templates.service';
import { dateWithoutTime } from '@utils/functions';
import dayjs from 'dayjs';
import { MatTooltip } from '@angular/material/tooltip';
import { MatIcon } from '@angular/material/icon';
import { DatePipe, NgIf } from '@angular/common';

const DUE_DATE_WARN_DAYS = 14;

export interface PartialDossier extends Partial<IDossier> {
    readonly dossierName: string;
    readonly dossierTemplateId: string;
    readonly dueDate?: string;
    readonly date?: string;
    readonly memberIds: List;
    readonly softDeletedTime?: string;
}

@Component({
    selector: 'redaction-dossier-name-column',
    templateUrl: './dossier-name-column.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
    standalone: true,
    imports: [MatTooltip, MatIcon, NgIf, DatePipe],
})
export class DossierNameColumnComponent {
    @Input() dossier: PartialDossier;
    @Input() dossierStats: DossierStats;

    constructor(private readonly _dossierTemplatesService: DossierTemplatesService) {}

    get approachingDueDate(): boolean {
        return this.#dueDateDaysDiff >= 0 && this.#dueDateDaysDiff <= DUE_DATE_WARN_DAYS;
    }

    get passedDueDate(): boolean {
        return this.#dueDateDaysDiff < 0;
    }

    get isSoftDeleted(): boolean {
        return !!this.dossier.softDeletedTime;
    }

    get #dueDateDaysDiff(): number {
        return dateWithoutTime(dayjs(this.dossier.dueDate)).diff(dateWithoutTime(dayjs()), 'day');
    }

    getDossierTemplateNameFor(dossierTemplateId: string): string {
        return this._dossierTemplatesService.find(dossierTemplateId)?.name || '-';
    }
}
