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

const DUE_DATE_WARN_DAYS = 14;

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

@Component({
    selector: 'redaction-dossier-name-column',
    templateUrl: './dossier-name-column.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
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;
    }

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

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