import { Deserializable } from './deserializable.model';
import { Serializable } from './serializable.model';

import { User } from './user.model';
import moment, {Moment} from "moment";

export class Base implements Deserializable, Serializable {
    id?: string;
    createdAt?: Moment;
    modifiedAt?: Moment;
    createdById?: string;
    modifiedById?: string;
    createdBy?: User;
    modifiedBy?: User;

    constructor(data: Partial<Base> = {}) {
        Object.assign(this, data);
    }

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    deserialize(input: any): this {
        if (input.createdAt) { input.createdAt = moment(input.createdAt).utc(true); }
        if (input.modifiedAt) { input.modifiedAt = moment(input.modifiedAt).utc(true); }
        if (input.createdBy) { input.createdBy = new User(input.createdBy); }
        if (input.modifiedBy) { input.modifiedBy = new User(input.modifiedBy); }
        Object.assign(this, input);
        return this;
    }

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    serialize() : any {
        return Object.assign({}, this);
    }
}
