import {
  Column,
  CreateDateColumn,
  Entity,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { App } from './app.entity';

@Entity('users')
export class User {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'google_id', unique: true, nullable: true, type: 'varchar' })
  googleId: string | null;

  @Column({ unique: true })
  email: string;

  @Column({ type: 'varchar', nullable: true })
  password: string | null;

  @Column()
  name: string;

  @Column({ type: 'varchar', nullable: true, name: 'profile_photo' })
  profilePhoto: string | null;

  @Column({ type: 'varchar', nullable: true, name: 'access_token' })
  accessToken: string | null;

  @Column({ type: 'varchar', nullable: true, name: 'refresh_token' })
  refreshToken: string | null;

  @Column({ type: 'bigint', nullable: true, name: 'token_expiry' })
  tokenExpiry: number | null;

  /** Whether to show uploader information on public install pages */
  @Column({ name: 'show_uploader_info', type: 'boolean', default: false })
  showUploaderInfo: boolean;

  /** User role: 'user' or 'admin' */
  @Column({ type: 'varchar', length: 20, default: 'user' })
  role: string;

  /** Whether the user account is active */
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

  /** Last time user was active (logged in or made an API call) */
  @Column({ name: 'last_active_at', type: 'timestamp', nullable: true })
  lastActiveAt: Date | null;

  /** Per-user build link expiry override. null = use global setting. */
  @Column({ name: 'build_link_expiry_days', type: 'float', nullable: true, default: null })
  buildLinkExpiryDays: number | null;

  /** Per-user AI release notes override. null = use global setting. */
  @Column({ name: 'ai_release_notes_enabled', type: 'boolean', nullable: true, default: null })
  aiReleaseNotesEnabled: boolean | null;

  /** Per-user max build size override in MB. null = use global setting. */
  @Column({ name: 'max_build_size_mb', type: 'int', nullable: true, default: null })
  maxBuildSizeMb: number | null;

  /** Per-user CI/CD override. null = use global setting. */
  @Column({ name: 'cicd_enabled', type: 'boolean', nullable: true, default: null })
  cicdEnabled: boolean | null;

  /** Per-user Slack integration override. null = use global setting. */
  @Column({ name: 'slack_enabled', type: 'boolean', nullable: true, default: null })
  slackEnabled: boolean | null;

  /** Slack workspace access token (for sending notifications to user's Slack) */
  @Column({ name: 'slack_access_token', type: 'varchar', nullable: true })
  slackAccessToken: string | null;

  /** Slack workspace ID */
  @Column({ name: 'slack_workspace_id', type: 'varchar', nullable: true })
  slackWorkspaceId: string | null;

  /** Slack webhook URL for incoming webhooks */
  @Column({ name: 'slack_webhook_url', type: 'varchar', nullable: true })
  slackWebhookUrl: string | null;

  /** Slack channel ID or name where notifications will be sent */
  @Column({ name: 'slack_channel_id', type: 'varchar', nullable: true })
  slackChannelId: string | null;

  /** Slack bot user ID */
  @Column({ name: 'slack_bot_user_id', type: 'varchar', nullable: true })
  slackBotUserId: string | null;

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

  @OneToMany(() => App, (app) => app.user)
  apps: App[];
}
