/**
 * WebhookEndpointLog Entity
 * Activity log for webhook endpoints
 * Compatible with erp-core endpoint-log.entity
 *
 * @module Webhooks
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { WebhookEndpoint } from './endpoint.entity';

export type WebhookLogType = 'config_changed' | 'activated' | 'deactivated' | 'verified' | 'error' | 'rate_limited' | 'created';

@Entity({ name: 'endpoint_logs', schema: 'webhooks' })
export class WebhookEndpointLog {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'endpoint_id', type: 'uuid' })
  endpointId: string;

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

  @Column({ name: 'log_type', type: 'varchar', length: 30 })
  logType: WebhookLogType;

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

  @Column({ name: 'details', type: 'jsonb', default: {} })
  details: Record<string, any>;

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

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

  @ManyToOne(() => WebhookEndpoint, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'endpoint_id' })
  endpoint: WebhookEndpoint;
}
