/**
 * Dashboard Entity
 * Configuración de dashboards personalizables
 *
 * @module Reports
 * @table reports.dashboards
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { DashboardWidget } from './dashboard-widget.entity';

export type DashboardType = 'corporate' | 'project' | 'department' | 'personal' | 'custom';

export type DashboardVisibility = 'private' | 'team' | 'department' | 'company';

@Entity({ schema: 'reports', name: 'dashboards' })
@Index(['tenantId', 'code'], { unique: true })
@Index(['tenantId'])
@Index(['dashboardType'])
@Index(['ownerId'])
@Index(['isActive'])
export class Dashboard {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  @Column({
    name: 'dashboard_type',
    type: 'varchar',
    length: 30,
    default: 'custom',
  })
  dashboardType: DashboardType;

  @Column({
    type: 'varchar',
    length: 20,
    default: 'private',
  })
  visibility: DashboardVisibility;

  @Column({
    name: 'owner_id',
    type: 'uuid',
    nullable: true,
    comment: 'User who owns this dashboard',
  })
  ownerId: string | null;

  @Column({
    name: 'fraccionamiento_id',
    type: 'uuid',
    nullable: true,
    comment: 'Project-specific dashboard',
  })
  fraccionamientoId: string | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Layout configuration (grid positions)',
  })
  layout: Record<string, any> | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Theme and styling configuration',
  })
  theme: Record<string, any> | null;

  @Column({
    name: 'refresh_interval',
    type: 'integer',
    default: 300,
    comment: 'Auto-refresh interval in seconds',
  })
  refreshInterval: number;

  @Column({
    name: 'default_date_range',
    type: 'varchar',
    length: 30,
    default: 'last_30_days',
    comment: 'Default date range filter',
  })
  defaultDateRange: string;

  @Column({
    name: 'default_filters',
    type: 'jsonb',
    nullable: true,
  })
  defaultFilters: Record<string, any> | null;

  @Column({
    name: 'allowed_roles',
    type: 'varchar',
    array: true,
    nullable: true,
    comment: 'Roles that can view this dashboard',
  })
  allowedRoles: string[] | null;

  @Column({
    name: 'is_default',
    type: 'boolean',
    default: false,
    comment: 'Default dashboard for type',
  })
  isDefault: boolean;

  @Column({
    name: 'is_active',
    type: 'boolean',
    default: true,
  })
  isActive: boolean;

  @Column({
    name: 'is_system',
    type: 'boolean',
    default: false,
    comment: 'System dashboard cannot be deleted',
  })
  isSystem: boolean;

  @Column({
    name: 'sort_order',
    type: 'integer',
    default: 0,
  })
  sortOrder: number;

  @Column({
    name: 'view_count',
    type: 'integer',
    default: 0,
  })
  viewCount: number;

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

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

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

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

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

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

  @Column({ name: 'deleted_by', type: 'uuid', nullable: true })
  deletedById: string | null;

  // Relations
  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'owner_id' })
  owner: User | null;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'created_by' })
  createdBy: User | null;

  @OneToMany(() => DashboardWidget, (w) => w.dashboard)
  widgets: DashboardWidget[];
}
