import { Injectable } from '@nestjs/common';

import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import { CONNECTION_NAME } from 'src/core/strings/constants/base.constants';
import { DataSource, Repository } from 'typeorm';
import {
  QueueItemModel,
  QueueModel,
  QueueOrderModel,
} from '../models/queue.model';
import { BaseReadService } from 'src/core/modules/data/service/base-read.service';
import { BaseDataService } from 'src/core/modules/data/service/base-data.service';

@Injectable()
export class QueueDataService extends BaseReadService<QueueModel> {
  constructor(
    @InjectRepository(QueueModel, CONNECTION_NAME.DEFAULT)
    private repo: Repository<QueueModel>,
  ) {
    super(repo);
  }
}

@Injectable()
export class QueueService extends BaseDataService<QueueModel> {
  constructor(
    @InjectRepository(QueueModel, CONNECTION_NAME.DEFAULT)
    private repo: Repository<QueueModel>,

    @InjectRepository(QueueItemModel, CONNECTION_NAME.DEFAULT)
    private item: Repository<QueueItemModel>,

    @InjectDataSource(CONNECTION_NAME.DEFAULT)
    private dataSource: DataSource,
  ) {
    super(repo);
  }

  async getTicketItems(ticket_id: string, item_id: string) {
    return this.item.findOneOrFail({
      where: {
        ticket_id,
        item_id,
      },
    });
  }

  async updateItemQty(item_id: string, qty: number): Promise<void> {
    const query = `UPDATE queue_items SET qty = qty - ${qty} WHERE id = '${item_id}'`;
    this.dataSource.query(query);
  }
}

@Injectable()
export class QueueOrderService extends BaseDataService<QueueOrderModel> {
  constructor(
    @InjectRepository(QueueOrderModel, CONNECTION_NAME.DEFAULT)
    private repo: Repository<QueueOrderModel>,
  ) {
    super(repo);
  }
}
