import { Injectable, inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Injectable({
  providedIn: 'root',
})
export class BettingService {
  private fb = inject(FormBuilder);

  createBetForm(): FormGroup {
    return this.fb.group({
      bet: ['', [Validators.required, Validators.min(1)]],
    });
  }

  updateBetFormValidators(form: FormGroup, balance: number): void {
    form.reset();
    form
      .get('bet')
      ?.setValidators([Validators.required, Validators.min(1), Validators.max(balance)]);
    form.get('bet')?.updateValueAndValidity();
  }

  calculateBetAmount(balance: number, percentage: number): number {
    return Math.floor(balance * percentage * 100) / 100;
  }

  isValidBet(betAmount: number, balance: number): boolean {
    return betAmount >= 1 && betAmount <= balance;
  }
}
