import { Model } from '@nozbe/watermelondb';
import { field, children, date } from '@nozbe/watermelondb/decorators';
import Message from './Message';

export default class Chat extends Model {
  static table = 'chats';

  static associations = {
    messages: { type: 'has_many' as const, foreignKey: 'chat_key' }
  };

  @field('chat_key') chatKey!: string | null;

  @field('chat_uid') chatUid?: number | null;
  @field('group_chat_token') groupChatToken?: string | null;

  @field('name') name!: string;
  @field('avatar') avatar?: string | null;
  @field('short') short!: string;
  @field('sent_by') sentBy!: number;
  @field('updated') updated!: number;
  @field('status') status!: number;
  @field('unread_count') unreadCount!: number;
  @field('last_message_id') lastMessageId!: number;
  @field('pin') pin!: number;
  @field('pin_order') pinOrder!: number;
  @field('archive') archive!: number;
  @field('archive_order') archiveOrder!: number;
  @field('attachement_name') attachmentName!: string;
  @field('encrypted') encrypted!: number;
  @field('muted') muted!: number;
  @field('user_type') userType?: string | null; // 'normal' | 'not_exist' | 'blocked'

  @field('can_send_messages') canSendMessages?: number;
  @field('is_admin') isAdmin?: number;
  @field('announcement') announcement?: number;

  @field('removed') removed?: boolean;
  @field('is_dirty') isDirty!: boolean;
  @field('dirty_actions') dirtyActions!: string | null;
  @date('deleted_at') deletedAt!: Date | null;

  @children('messages') messages!: Message[];
}
