/**
 * UserProfile Entity
 * Role-based profile with module access, tools and pricing
 * Compatible with erp-core user-profile.entity
 *
 * @module Profiles
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  DeleteDateColumn,
  Index,
  OneToMany,
  Unique,
} from 'typeorm';
import { ProfileTool } from './profile-tool.entity';
import { ProfileModule } from './profile-module.entity';

@Entity({ name: 'user_profiles', schema: 'auth' })
@Unique(['tenantId', 'code'])
export class UserProfile {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'tenant_id', type: 'uuid', nullable: true })
  tenantId: string;

  @Index()
  @Column({ type: 'varchar', length: 10 })
  code: string;

  @Column({ type: 'varchar', length: 100 })
  name: string;

  @Column({ type: 'text', nullable: true })
  description: string;

  @Column({ name: 'is_system', type: 'boolean', default: false })
  isSystem: boolean;

  @Column({ type: 'varchar', length: 20, nullable: true })
  color: string;

  @Column({ type: 'varchar', length: 50, nullable: true })
  icon: string;

  @Column({ name: 'base_permissions', type: 'jsonb', default: [] })
  basePermissions: string[];

  @Column({ name: 'available_modules', type: 'text', array: true, default: [] })
  availableModules: string[];

  @Column({ name: 'monthly_price', type: 'decimal', precision: 10, scale: 2, default: 0 })
  monthlyPrice: number;

  @Column({ name: 'included_platforms', type: 'text', array: true, default: ['web'] })
  includedPlatforms: string[];

  @Column({ name: 'default_tools', type: 'text', array: true, default: [] })
  defaultTools: string[];

  @Column({ name: 'feature_flags', type: 'jsonb', default: {} })
  featureFlags: Record<string, boolean>;

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

  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy: string;

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

  @Column({ name: 'updated_by', type: 'uuid', nullable: true })
  updatedBy: string;

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date;

  @OneToMany(() => ProfileTool, (tool) => tool.profile, { cascade: true })
  tools: ProfileTool[];

  @OneToMany(() => ProfileModule, (module) => module.profile, { cascade: true })
  modules: ProfileModule[];
}
