import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { User } from './user.entity';

@Entity('sessions', { schema: 'auth' })
@Index(['user_id'])
@Index(['expires_at'])
export class Session {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column('uuid')
  user_id: string;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' })
  user: User;

  @Column({ length: 500 })
  refresh_token_hash: string;

  @Column({ length: 100, nullable: true })
  device_info: string | null;

  @Column({ length: 50, nullable: true })
  ip_address: string | null;

  @Column({ type: 'timestamptz' })
  expires_at: Date;

  @CreateDateColumn({ type: 'timestamptz' })
  created_at: Date;

  @Column({ type: 'timestamptz', nullable: true })
  last_used_at: Date | null;

  @Column({ default: true })
  is_active: boolean;
}
