import { DataSource } from 'typeorm';
import { User } from '../../../repositories/users/entity/user.entity';
import { Account } from '../../../repositories/accounts/entity/account.entity';
import {
  License,
  CardLicense,
  CardLicenseIssue,
} from '../../../repositories/licenses/entity/license.entity';

export const createAccount = async (
  datasource: DataSource,
): Promise<{ accountId: number }> => {
  const { identifiers } = await datasource.getRepository(Account).insert({
    tier: 1,
    country: 'JP',
    delegation_permission: false,
    locked: false,
    company_name: 'test inc.',
    verified: true,
    deleted_at: '',
    created_by: 'test_runner',
    created_at: new Date(),
    updated_by: 'updater',
    updated_at: new Date(),
  });
  const account = identifiers.pop() as Account;
  return { accountId: account.id };
};

export const createUser = async (
  datasource: DataSource,
  accountId: number,
  external_id: string,
  role: string,
  author_id?: string | undefined,
): Promise<{ userId: number; externalId: string }> => {
  const { identifiers } = await datasource.getRepository(User).insert({
    account_id: accountId,
    external_id: external_id,
    role: role,
    accepted_terms_version: '1.0',
    author_id: author_id,
    email_verified: true,
    auto_renew: true,
    license_alert: true,
    notification: true,
    encryption: false,
    prompt: false,
    created_by: 'test_runner',
    created_at: new Date(),
    updated_by: 'updater',
    updated_at: new Date(),
  });
  const user = identifiers.pop() as User;
  return { userId: user.id, externalId: external_id };
};

export const createLicense = async (
  datasource: DataSource,
  licenseId: number,
  accountId: number,
): Promise<void> => {
  const { identifiers } = await datasource.getRepository(License).insert({
    id: licenseId,
    expiry_date: null,
    account_id: accountId,
    type: 'card',
    status: 'Unallocated',
    allocated_user_id: null,
    order_id: null,
    deleted_at: null,
    delete_order_id: null,
    created_by: 'test_runner',
    created_at: new Date(),
    updated_by: 'updater',
    updated_at: new Date(),
  });
  identifiers.pop() as License;
};

export const createCardLicense = async (
  datasource: DataSource,
  licenseId: number,
  issueId: number,
  cardLicenseKey: string,
): Promise<void> => {
  const { identifiers } = await datasource.getRepository(CardLicense).insert({
    license_id: licenseId,
    issue_id: issueId,
    card_license_key: cardLicenseKey,
    activated_at: null,
    created_by: 'test_runner',
    created_at: new Date(),
    updated_by: 'updater',
    updated_at: new Date(),
  });
  identifiers.pop() as CardLicense;
};

export const createCardLicenseIssue = async (
  datasource: DataSource,
  issueId: number,
): Promise<void> => {
  const { identifiers } = await datasource
    .getRepository(CardLicenseIssue)
    .insert({
      id: issueId,
      issued_at: new Date(),
      created_by: 'test_runner',
      created_at: new Date(),
      updated_by: 'updater',
      updated_at: new Date(),
    });
  identifiers.pop() as CardLicenseIssue;
};

export const selectCardLicensesCount = async (
  datasource: DataSource,
): Promise<{ count: number }> => {
  const count = await datasource.getRepository(CardLicense).count();
  return { count: count };
};

export const selectCardLicense = async (
  datasource: DataSource,
  cardLicenseKey: string,
): Promise<{ cardLicense: CardLicense }> => {
  const cardLicense = await datasource.getRepository(CardLicense).findOne({
    where: {
      card_license_key: cardLicenseKey,
    },
  });
  return { cardLicense };
};

export const selectLicense = async (
  datasource: DataSource,
  id: number,
): Promise<{ license: License }> => {
  const license = await datasource.getRepository(License).findOne({
    where: {
      id: id,
    },
  });
  return { license };
};
