import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { ZardFormModule } from '@shared/components/form/form.module';
import { ZardInputDirective } from '@shared/components/input/input.directive';
import { AgentLimit } from 'src/app/core/interfaces/agent-limit';

@Component({
  selector: 'app-limit-form',
  standalone: true,
  templateUrl: './limit-form.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [CommonModule, ReactiveFormsModule, ZardFormModule, ZardInputDirective],
})
export class LimitForm {
  @Output() save = new EventEmitter<AgentLimit>();
  @Output() cancel = new EventEmitter<void>();

  private _value?: AgentLimit;
  @Input() set value(v: AgentLimit | undefined) {
    this._value = v;
    this.hydrate(v);
  }
  get value(): AgentLimit | undefined {
    return this._value;
  }

  form: FormGroup;
  submitted = false;

  constructor(private fb: FormBuilder) {
    this.form = this.fb.group({
      code: ['', Validators.required],
      configCode: ['', Validators.required],
      nom: ['', Validators.required],
      isDefault: [false],
      actif: [true],
      betMin: [0],
      betMax: [0],
      maxBet: [0],
      maxDisburseBet: [0],
      airtimeMin: [0],
      airtimeMax: [0],
    });
  }

  error(control: string): string {
    const c = this.form.get(control);
    if (!c) return '';
    const invalid = c.invalid && (c.touched || this.submitted);
    if (!invalid) return '';
    const e = c.errors || {};
    if (e['required']) return 'Ce champ est obligatoire';
    return 'Valeur invalide';
  }

  private hydrate(v?: AgentLimit) {
    if (!v) {
      this.form.reset({
        code: '',
        configCode: '',
        nom: '',
        isDefault: false,
        actif: true,
        betMin: 0,
        betMax: 0,
        maxBet: 0,
        maxDisburseBet: 0,
        airtimeMin: 0,
        airtimeMax: 0,
      });
      return;
    }
    this.form.reset({
      code: v.code,
      configCode: v.configCode,
      nom: v.nom,
      isDefault: v.isDefault,
      actif: v.actif,
      betMin: v.betMin ?? 0,
      betMax: v.betMax ?? 0,
      maxBet: v.maxBet ?? 0,
      maxDisburseBet: v.maxDisburseBet ?? 0,
      airtimeMin: v.airtimeMin ?? 0,
      airtimeMax: v.airtimeMax ?? 0,
    });
  }

  onSubmit() {
    this.submitted = true;
    if (this.form.invalid) {
      this.form.markAllAsTouched();
      return;
    }
    const raw = this.form.getRawValue() as any;
    const payload: AgentLimit = {
      id: this.value?.id ?? '',
      code: raw.code,
      configCode: raw.configCode,
      nom: raw.nom,
      isDefault: !!raw.isDefault,
      actif: !!raw.actif,
      betMin: +raw.betMin,
      betMax: +raw.betMax,
      maxBet: +raw.maxBet,
      maxDisburseBet: +raw.maxDisburseBet,
      airtimeMin: +raw.airtimeMin,
      airtimeMax: +raw.airtimeMax,
    };
    this.save.emit(payload);
  }
}
