import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToOne,
  JoinColumn,
  OneToMany,
} from 'typeorm';
import { Occupancy } from './occupancy.entity';
import { PersonalExpense } from './personal-expense.entity';
import { Bill } from './bill.entity';
import { Organization } from './organization.entity';
import { User } from './user.entity';

@Entity('students')
export class Student {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 50 })
  name: string;
  @Column({ name: 'student_no', length: 30, nullable: true })
  studentNo: string;

  @Column({ length: 20, nullable: true })
  phone: string;

  @Column({ name: 'id_number', length: 30, nullable: true })
  idNumber: string;

  @Column({ length: 10, nullable: true })
  gender: string;

  @Column({ length: 20, nullable: true })
  ethnicity: string;

  @Column({ name: 'emergency_contact', length: 50, nullable: true })
  emergencyContact: string;

  @Column({ name: 'emergency_phone', length: 20, nullable: true })
  emergencyPhone: string;

  @Column({ type: 'varchar', length: 20, default: 'active' })
  status: string;

  @Column({ length: 50, nullable: true })
  supervisor: string;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;

  @OneToMany(() => Occupancy, (o) => o.student)
  occupancies: Occupancy[];

  @Column({ name: 'user_id', type: 'integer', nullable: true, unique: true })
  userId: number;

  @OneToOne(() => User, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'user_id' })
  user: User;

  @Column({ name: 'organization_id', type: 'integer', nullable: true })
  organizationId: number;

  @ManyToOne(() => Organization, { nullable: true })
  @JoinColumn({ name: 'organization_id' })
  organization: Organization;

  @OneToMany(() => PersonalExpense, (e) => e.student)
  personalExpenses: PersonalExpense[];

  @OneToMany(() => Bill, (b) => b.student)
  bills: Bill[];
}
