import {
  Entity, PrimaryColumn, Column, CreateDateColumn, UpdateDateColumn,
  OneToMany, OneToOne,
} from 'typeorm';
import { EventType } from './EventType.entity';
import { AvailabilitySchedule } from './AvailabilitySchedule.entity';
import { Booking } from './Booking.entity';
import { CalendarConnection } from './CalendarConnection.entity';
import { EmbedConfig } from './EmbedConfig.entity';
import { ApiKey } from './ApiKey.entity';
import { UserEmailSettings } from './UserEmailSettings.entity';
import { Location } from './Location.entity';

@Entity('users')
export class User {
  @PrimaryColumn('char', { length: 36 })
  id!: string;

  @Column({ length: 255, unique: true })
  email!: string;

  @Column({ name: 'password_hash', type: 'varchar', length: 255, nullable: true })
  passwordHash!: string | null;

  @Column({
    name: 'auth_provider',
    type: 'enum',
    enum: ['email', 'google', 'linkedin'],
    default: 'email',
  })
  authProvider!: 'email' | 'google' | 'linkedin';

  @Column({ name: 'oauth_subject', type: 'varchar', length: 255, nullable: true })
  oauthSubject!: string | null;

  @Column({ name: 'password_changed_at', type: 'datetime', precision: 3, nullable: true })
  passwordChangedAt!: Date | null;

  @Column({ name: 'must_change_password', type: 'tinyint', width: 1, default: 0 })
  mustChangePassword!: boolean;

  @Column({ name: 'full_name', length: 255 })
  fullName!: string;

  @Column({ length: 64, unique: true })
  username!: string;

  @Column({ length: 64, default: 'UTC' })
  timezone!: string;

  @Column({ name: 'avatar_url', type: 'varchar', length: 512, nullable: true })
  avatarUrl!: string | null;

  @Column({ name: 'banner_url', type: 'varchar', length: 512, nullable: true })
  bannerUrl!: string | null;

  @Column({ name: 'logo_url', type: 'varchar', length: 512, nullable: true })
  logoUrl!: string | null;

  @Column({ type: 'varchar', length: 255, nullable: true })
  tagline!: string | null;

  @Column({ type: 'varchar', length: 255, nullable: true })
  headline!: string | null;

  @Column({ type: 'text', nullable: true })
  bio!: string | null;

  @Column({ name: 'job_title', type: 'varchar', length: 255, nullable: true })
  jobTitle!: string | null;

  @Column({ type: 'varchar', length: 255, nullable: true })
  company!: string | null;

  @Column({ type: 'varchar', length: 255, nullable: true })
  location!: string | null;

  @Column({ name: 'linkedin_url', type: 'varchar', length: 512, nullable: true })
  linkedinUrl!: string | null;

  @Column({ name: 'email_verified', type: 'tinyint', width: 1, default: 0 })
  emailVerified!: boolean;

  @CreateDateColumn({ name: 'created_at', type: 'datetime', precision: 3 })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'datetime', precision: 3 })
  updatedAt!: Date;

  @OneToMany(() => EventType, (et) => et.user)
  eventTypes!: EventType[];

  @OneToMany(() => AvailabilitySchedule, (s) => s.user)
  availabilitySchedules!: AvailabilitySchedule[];

  @OneToMany(() => Booking, (b) => b.user)
  bookings!: Booking[];

  @OneToMany(() => CalendarConnection, (c) => c.user)
  calendarConnections!: CalendarConnection[];

  @OneToMany(() => EmbedConfig, (e) => e.user)
  embedConfigs!: EmbedConfig[];

  @OneToMany(() => ApiKey, (k) => k.user)
  apiKeys!: ApiKey[];

  @OneToMany(() => Location, (l) => l.user)
  locations!: Location[];

  @OneToOne(() => UserEmailSettings, (s) => s.user)
  emailSettings!: UserEmailSettings;
}
