/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */

import {
    Column,
    CreateDateColumn,
    Entity, JoinColumn, ManyToOne, OneToMany,
    PrimaryGeneratedColumn,
    UpdateDateColumn
} from "typeorm";
import {ChatMember} from "../member";
import {AuthContact} from "../../auth/contact";

export enum ChatRoomType {
    PRIVATE = 'private',
    CHANNEL = 'channel',
    GROUP = 'group'
}

@Entity()
export class ChatRoom {
    @PrimaryGeneratedColumn("uuid")
    id: string;

    @Column({type: 'varchar', length: 100, nullable: true})
    name: string;

    @Column({type: "varchar", nullable: true})
    description: string;

    @Column({
        type: "enum",
        enum: ChatRoomType,
        default: ChatRoomType.CHANNEL
    })
    type: string;

    @Column({type: "boolean", default: false})
    encryption: boolean;

    @Column({type: 'boolean', default: false})
    protected: boolean;

    @Column({type: "varchar", nullable: true})
    password: string;

    @OneToMany(() => ChatMember, (chatMember) => chatMember.room, {onDelete: "CASCADE"})
    members: ChatMember[];

    /*
    Meta
     */
    @Column({type: "varchar", nullable: true, default: null})
    contact_id: string;

    @ManyToOne(() => AuthContact, {nullable: true, onDelete: "SET NULL"})
    @JoinColumn({name: 'contact_id'})
    contact: AuthContact;

    @CreateDateColumn()
    created_at: string;

    @UpdateDateColumn()
    updated_at: string
}
