import {
  Entity,
  Column,
  PrimaryColumn,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
} from 'typeorm';
import { User } from './user.entity';
import { Family } from './family.entity';

export enum FamilyRole {
  PARENT = 'parent',
  CAREGIVER = 'caregiver',
  VIEWER = 'viewer',
}

export interface FamilyPermissions {
  canAddChildren: boolean;
  canEditChildren: boolean;
  canLogActivities: boolean;
  canViewReports: boolean;
  canInviteMembers: boolean;
}

@Entity('family_members')
export class FamilyMember {
  @PrimaryColumn({ name: 'user_id', length: 20 })
  userId: string;

  @PrimaryColumn({ name: 'family_id', length: 20 })
  familyId: string;

  @Column({
    type: 'varchar',
    length: 20,
    enum: FamilyRole,
  })
  role: FamilyRole;

  @Column({
    type: 'jsonb',
    default: {
      canAddChildren: false,
      canEditChildren: false,
      canLogActivities: true,
      canViewReports: true,
    },
  })
  permissions: FamilyPermissions;

  @CreateDateColumn({ name: 'joined_at' })
  joinedAt: Date;

  @ManyToOne(() => User, (user) => user.familyMemberships, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'user_id' })
  user: User;

  @ManyToOne(() => Family, (family) => family.members, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'family_id' })
  family: Family;
}
