import { ApiAttribute } from "./ApiAttribute";
import { Entity, Column, ManyToMany, JoinTable, OneToMany } from "typeorm";
import { EntityBase } from "./base/Base";
import { ApiKey } from "./ApiKey";
import { ApiHistory } from "./ApiHistory";

@Entity("apiName")
export class ApiName extends EntityBase {
  @Column({
    nullable: true,
    comment: "ชื่อ",
    length: 255,
    default: null,
  })
  name: string;

  @Column({
    nullable: true,
    comment: "path",
    length: 255,
    default: null,
  })
  pathApi: string;

  @Column({
    nullable: true,
    comment: "method",
    length: 255,
    default: null,
  })
  methodApi: string;

  @Column({
    nullable: false,
    comment: "code สำหรับการเรียก API",
    length: 8,
  })
  code: string;

  @Column({
    nullable: false,
    comment: "code ระบบสำหรับการเรียก API",
    length: 50,
    default: "registry",
  })
  system: string;

  @Column({
    nullable: false,
    comment: "สถานะการใช้งาน",
    type: "boolean",
    default: false,
  })
  isActive: boolean;

  @ManyToMany(() => ApiKey, (apiKey) => apiKey.apiNames)
  @JoinTable()
  apiKeys: ApiKey[];

  @OneToMany(() => ApiHistory, (v) => v.apiName)
  apiHistorys: ApiHistory[];

  @OneToMany(() => ApiAttribute, (v) => v.apiName)
  apiAttributes: ApiAttribute[];
}

export class CreateApi {
  @Column()
  name: string;

  @Column()
  methodApi: "GET" | "POST" | "PUT";

  @Column()
  system: string;

  @Column()
  isActive: boolean;

  @Column()
  apiAttributes: CreateApiAttribute[];
}

export class CreateApiAttribute {
  @Column()
  tbName: string;

  @Column()
  propertyKey: string[];
}
