import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsInt, IsOptional, Min } from 'class-validator';

export class CreateAccountRequest {
  @ApiProperty()
  companyName: string;
  @ApiProperty({
    description: '国名(ISO 3166-1 alpha-2)',
    minLength: 2,
    maxLength: 2,
  })
  country: string;
  @ApiProperty({ nullable: true })
  @IsInt()
  @IsOptional()
  dealerAccountId?: number;
  @ApiProperty()
  adminName: string;
  @ApiProperty()
  @IsEmail()
  adminMail: string;
  @ApiProperty()
  adminPassword: string;
  @ApiProperty({ description: '同意済み利用規約のバージョン' })
  acceptedTermsVersion: string;
  @ApiProperty({ description: 'reCAPTCHA Token' })
  token: string;
}

export class CreateAccountResponse {}

export class GetLicenseSummaryRequest {
  @ApiProperty()
  accountId: number;
}
export class LicenseSummaryInfo {
  totalLicense: number;
  allocatedLicense: number;
  reusableLicense: number;
  freeLicense: number;
  expiringSoonLicense: number;
  issueRequesting: number;
  numberOfRequesting: number;
  allocatableLicenseWithMargin: number;
}
export class GetLicenseSummaryResponse {
  @ApiProperty()
  totalLicense: number;

  @ApiProperty()
  allocatedLicense: number;

  @ApiProperty()
  reusableLicense: number;

  @ApiProperty()
  freeLicense: number;

  @ApiProperty()
  expiringWithin14daysLicense: number;

  @ApiProperty()
  issueRequesting: number;

  @ApiProperty()
  numberOfRequesting: number;

  @ApiProperty()
  shortage: number;

  @ApiProperty()
  storageSize: number;

  @ApiProperty()
  usedSize: number;

  @ApiProperty()
  isStorageAvailable: boolean;
}

export class Account {
  @ApiProperty()
  accountId: number;
}

export class GetMyAccountResponse {
  @ApiProperty({ type: Account })
  account: Account;
}

export class Typist {
  @ApiProperty({
    description: 'TypistのユーザーID',
  })
  id: number;

  @ApiProperty({ description: 'Typistのユーザー名' })
  name: string;
}

export class GetTypistsResponse {
  @ApiProperty({ type: [Typist] })
  typists: Typist[];
}

export class TypistGroup {
  @ApiProperty({
    description: 'TypistGroupのID',
  })
  id: number;

  @ApiProperty({ description: 'TypistGroup名' })
  name: string;
}

export class GetTypistGroupsResponse {
  @ApiProperty({ type: [TypistGroup] })
  typistGroups: TypistGroup[];
}

export class CreatePartnerAccountRequest {
  @ApiProperty()
  companyName: string;
  @ApiProperty({
    description: '国名(ISO 3166-1 alpha-2)',
    minLength: 2,
    maxLength: 2,
  })
  country: string;
  @ApiProperty()
  adminName: string;
  @ApiProperty()
  @IsEmail()
  email: string;
}

export class CreatePartnerAccountResponse {}

export class GetPartnerLicensesRequest {
  @ApiProperty()
  @IsInt()
  limit: number;
  @ApiProperty()
  @IsInt()
  offset: number;
  @ApiProperty()
  accountId: number;
}

export class PartnerLicenseInfo {
  @ApiProperty({ description: 'アカウントID' })
  accountId: number;

  @ApiProperty({ description: '階層' })
  tier: number;

  @ApiProperty({ description: 'アカウント名' })
  companyName: string;

  @ApiProperty({
    description: '保有している有効期限が未設定あるいは有効期限内のライセンス数',
  })
  stockLicense: number;

  @ApiProperty({
    description:
      '子アカウントからの、未発行状態あるいは発行キャンセルされた注文の総ライセンス数',
  })
  issuedRequested: number;

  @ApiProperty({ description: '不足数（{Stock license} - {Issue Requested}）' })
  shortage: number;

  @ApiProperty({
    description:
      '未発行状態あるいは発行キャンセルされた注文の総ライセンス数（=IssueRequestingのStatusの注文の総ライセンス数）',
  })
  issueRequesting: number;
}

export class GetPartnerLicensesResponse {
  @ApiProperty()
  total: number;
  @ApiProperty({ type: PartnerLicenseInfo })
  ownPartnerLicense: PartnerLicenseInfo;
  @ApiProperty({ type: [PartnerLicenseInfo] })
  childrenPartnerLicenses: PartnerLicenseInfo[];
}

// RepositoryからPartnerLicenseInfoに関する情報を取得する際の型
export type PartnerLicenseInfoForRepository = Omit<
  PartnerLicenseInfo,
  'shortage'
>;

export class GetOrderHistoriesRequest {
  @ApiProperty({ description: '取得件数' })
  @IsInt()
  @Min(0)
  limit: number;
  @ApiProperty({ description: '開始位置' })
  @IsInt()
  @Min(0)
  offset: number;
  @ApiProperty({ description: 'アカウントID' })
  accountId: number;
}

export class LicenseOrder {
  @ApiProperty({ description: '注文日付' })
  orderDate: string;
  @ApiProperty({ description: '発行日付' })
  issueDate: string;
  @ApiProperty({ description: '注文数' })
  numberOfOrder: number;
  @ApiProperty({ description: 'POナンバー' })
  poNumber: string;
  @ApiProperty({ description: '注文状態' })
  status: string;
}
export class GetOrderHistoriesResponce {
  @ApiProperty({ description: '合計件数' })
  total: number;
  @ApiProperty({ type: [LicenseOrder] })
  orderHistories: LicenseOrder[];
}
