/**
 * CustomPermission Entity
 * Permisos personalizados y temporales para usuarios
 *
 * @module Admin
 * @table admin.custom_permissions
 */

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

export type PermissionAction = 'create' | 'read' | 'update' | 'delete' | 'approve' | 'export' | 'import';

@Entity({ schema: 'admin', name: 'custom_permissions' })
@Index(['tenantId'])
@Index(['userId'])
@Index(['module'])
@Index(['isActive'])
@Index(['validUntil'])
@Index(['tenantId', 'userId', 'module'])
export class CustomPermission {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'user_id', type: 'uuid' })
  userId: string;

  @Column({
    type: 'varchar',
    length: 100,
    comment: 'Module this permission applies to',
  })
  module: string;

  @Column({
    type: 'varchar',
    array: true,
    comment: 'Actions allowed',
  })
  actions: PermissionAction[];

  @Column({
    name: 'resource_type',
    type: 'varchar',
    length: 100,
    nullable: true,
    comment: 'Specific resource type (e.g., Estimacion, Proyecto)',
  })
  resourceType: string | null;

  @Column({
    name: 'resource_id',
    type: 'uuid',
    nullable: true,
    comment: 'Specific resource ID (null = all resources)',
  })
  resourceId: string | null;

  @Column({
    name: 'fraccionamiento_id',
    type: 'uuid',
    nullable: true,
    comment: 'Project scope (null = all projects)',
  })
  fraccionamientoId: string | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Additional conditions for permission',
  })
  conditions: Record<string, any> | null;

  @Column({
    type: 'text',
    nullable: true,
    comment: 'Reason for granting this permission',
  })
  reason: string | null;

  @Column({
    name: 'valid_from',
    type: 'timestamptz',
    default: () => 'CURRENT_TIMESTAMP',
  })
  validFrom: Date;

  @Column({
    name: 'valid_until',
    type: 'timestamptz',
    nullable: true,
    comment: 'Expiration date (null = permanent)',
  })
  validUntil: Date | null;

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

  @Column({
    name: 'granted_by',
    type: 'uuid',
  })
  grantedById: string;

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

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

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

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

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

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' })
  user: User;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'granted_by' })
  grantedBy: User;

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