import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  OneToMany,
} from 'typeorm';
import { DashboardWidget } from './dashboard-widget.entity';

/**
 * Dashboard Entity (schema: reports.dashboards)
 *
 * Custom dashboards containing widgets for data visualization.
 * Supports multi-tenant isolation and role-based access.
 */
@Entity({ name: 'dashboards', schema: 'reports' })
export class Dashboard {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

  @Column({ name: 'slug', type: 'varchar', length: 100, nullable: true })
  slug: string | null;

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

  @Column({ name: 'layout_config', type: 'jsonb', default: '{"columns": 12, "rowHeight": 80}' })
  layoutConfig: Record<string, any>;

  @Index()
  @Column({ name: 'is_default', type: 'boolean', default: false })
  isDefault: boolean;

  @Column({ name: 'is_public', type: 'boolean', default: false })
  isPublic: boolean;

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

  @Index()
  @Column({ name: 'owner_id', type: 'uuid' })
  ownerId: string;

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

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

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

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