import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { PaymentMethod } from '../../entities/payment-method.entity';

@Injectable()
export class PaymentMethodRepository {
  constructor(
    @InjectRepository(PaymentMethod)
    private paymentMethodRepository: Repository<PaymentMethod>,
  ) {}

  async createData(request: Partial<PaymentMethod>): Promise<PaymentMethod> {
    const entity = this.paymentMethodRepository.create(request);
    const saved = await this.paymentMethodRepository.save(entity);
    if (!saved) {
      throw new HttpException('Failed to create payment method.', HttpStatus.UNPROCESSABLE_ENTITY);
    }
    return saved;
  }
}
