import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity('modules')
export class ModuleEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 150 })
  name: string;

  @Column({ length: 100, unique: true })
  slug: string;

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

  @Column({ length: 100 })
  api_prefix: string;

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

  // ══════════════════════════════════════════
  //  NEW: Dynamic UI Configuration
  // ══════════════════════════════════════════

  /**
   * JSON array of button definitions for this module.
   * Example:
   * [
   *   { "key": "create", "label": "New Client", "icon": "➕", "color": "#16a34a", "position": "toolbar" },
   *   { "key": "edit", "label": "Edit", "icon": "✏️", "color": "#3b82f6", "position": "row" },
   *   { "key": "delete", "label": "Delete", "icon": "🗑", "color": "#ef4444", "position": "row" },
   *   { "key": "export", "label": "Export", "icon": "📥", "color": "#8b5cf6", "position": "toolbar" }
   * ]
   *
   * position: "toolbar" = top of page, "row" = per-row action, "both" = both places
   */
  @Column({ type: 'json', nullable: true })
  button_config: ButtonConfig[] | null;

  /**
   * JSON array of column definitions for this module's data table.
   * Example:
   * [
   *   { "key": "name", "label": "Client Name", "type": "text", "sortable": true, "default_visible": true },
   *   { "key": "email", "label": "Email", "type": "text", "sortable": true, "default_visible": true },
   *   { "key": "balance", "label": "Balance", "type": "currency", "sortable": true, "default_visible": true },
   *   { "key": "status", "label": "Status", "type": "badge", "sortable": true, "default_visible": true },
   *   { "key": "created_at", "label": "Created", "type": "date", "sortable": true, "default_visible": false }
   * ]
   *
   * type: "text" | "currency" | "date" | "badge" | "boolean" | "link" | "image"
   */
  @Column({ type: 'json', nullable: true })
  column_config: ColumnConfig[] | null;

  /**
   * API endpoint for fetching table data (used by generic data table).
   * Example: "/api/clients" or "/api/proposals"
   */
  @Column({ type: 'varchar', length: 255, nullable: true })
  table_endpoint: string | null;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;
}

// ══════════════════════════════════════════
//  TypeScript Interfaces for JSON columns
// ══════════════════════════════════════════

export interface ButtonConfig {
  key: string;          // permission action key (e.g., "create", "delete", "bulk_import")
  label: string;        // display label (e.g., "New Client")
  icon?: string;        // emoji or icon name
  color?: string;       // hex color (e.g., "#16a34a")
  position: 'toolbar' | 'row' | 'both';  // where to show
  order?: number;       // display order (lower = first)
  confirm?: boolean;    // show confirmation dialog before action
  confirm_message?: string; // custom confirmation message
}

export interface ColumnConfig {
  key: string;           // field name from API response (e.g., "email")
  label: string;         // display header (e.g., "Email Address")
  type: 'text' | 'currency' | 'date' | 'badge' | 'boolean' | 'link' | 'image' | 'number';
  sortable?: boolean;    // allow sorting
  default_visible?: boolean; // visible by default (can be overridden by conditions)
  width?: string;        // column width (e.g., "150px", "auto")
  order?: number;        // display order
  format?: string;       // format hint (e.g., "AED" for currency, "DD/MM/YYYY" for date)
  badge_colors?: Record<string, string>; // status → color map for badge type
}