import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { BaseCoreModel } from 'src/core/modules/data/model/base-core.model';
import {
  TransactionBundlingItemEntity,
  TransactionItemEntity,
} from '../../domain/entities/transaction-item.entity';
import { TransactionModel } from './transaction.model';
import { RefundItemModel } from 'src/modules/transaction/refund/data/models/refund-item.model';
import { TransactionTaxEntity } from '../../domain/entities/transaction-tax.entity';
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';

@Entity(TABLE_NAME.TRANSACTION_ITEM)
export class TransactionItemModel
  extends BaseCoreModel<TransactionItemEntity>
  implements TransactionItemEntity
{
  // item data
  @Column('varchar', { name: 'item_id', nullable: true })
  item_id: string;

  @Column('varchar', { name: 'qr_image_url', nullable: true })
  qr_image_url: string;

  @Column('varchar', { name: 'item_name', nullable: true })
  item_name: string;

  @Column('varchar', { name: 'item_type', nullable: true })
  item_type: string;

  @Column('bigint', { name: 'item_price', nullable: true })
  item_price: number;

  @Column('bigint', { name: 'item_hpp', nullable: true })
  item_hpp: number;

  @Column('varchar', { name: 'item_category_id', nullable: true })
  item_category_id: string;

  @Column('varchar', { name: 'item_category_name', nullable: true })
  item_category_name: string;

  @Column('json', { name: 'item_bundlings', nullable: true })
  item_bundlings: string;

  // item tenant data
  @Column('varchar', { name: 'item_tenant_id', nullable: true })
  item_tenant_id: string;

  @Column('varchar', { name: 'item_tenant_name', nullable: true })
  item_tenant_name: string;

  @Column('decimal', { name: 'item_tenant_share_margin', nullable: true })
  item_tenant_share_margin: number;

  @Column('decimal', { nullable: true })
  total_net_price: number;

  @Column('decimal', { nullable: true })
  discount_value: number;

  // calculation data
  @Column('decimal', { name: 'total_price', nullable: true })
  total_price: number;

  @Column('decimal', { name: 'total_hpp', nullable: true })
  total_hpp: number;

  @Column('decimal', { name: 'total_profit', nullable: true })
  total_profit: number;

  @Column('decimal', { name: 'total_share_tenant', nullable: true })
  total_share_tenant: number;

  @Column('decimal', { name: 'total_profit_share', nullable: true })
  total_profit_share: number;

  @Column('decimal', { nullable: true })
  payment_total_dpp: number;

  @Column('decimal', { nullable: true })
  payment_total_tax: number;

  @Column('int', { name: 'qty', nullable: true })
  qty: number;

  @Column('int', { name: 'qty_remaining', nullable: true })
  qty_remaining: number;

  @Column('json', { name: 'taxes', nullable: true })
  taxes: string;

  @Column('varchar', { name: 'transaction_id', nullable: true })
  transaction_id: string;
  @ManyToOne(() => TransactionModel, (model) => model.items, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'transaction_id' })
  transaction: TransactionModel;

  // relations to refund
  @OneToMany(() => RefundItemModel, (model) => model.transaction_item, {
    cascade: true,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  refunds: RefundItemModel[];

  @Column('boolean', { name: 'breakdown_bundling', default: false })
  breakdown_bundling: boolean;

  @OneToMany(
    () => TransactionItemBreakdownModel,
    (model) => model.transaction_item,
    {
      cascade: true,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
  )
  bundling_items: TransactionItemBreakdownModel[];

  @OneToMany(() => TransactionItemTaxModel, (model) => model.transaction, {
    cascade: true,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  item_taxes: TransactionItemTaxModel[];

  @ManyToOne(() => ItemModel)
  @JoinColumn({ name: 'item_id' })
  item: ItemModel;
}

@Entity(TABLE_NAME.TRANSACTION_ITEM_BREAKDOWN)
export class TransactionItemBreakdownModel extends BaseCoreModel<TransactionBundlingItemEntity> {
  @Column('varchar')
  item_id: string;

  @Column('varchar')
  item_name: string;

  @Column('bigint', { nullable: true })
  hpp: number;

  @Column('bigint', { nullable: true })
  base_price: number;

  @Column('bigint', { nullable: true })
  item_rates: number;

  @Column('decimal', { nullable: true })
  total_net_price: number;

  @Column('decimal', { nullable: true })
  discount_value: number;

  // calculation data
  @Column('decimal', { name: 'total_price', nullable: true })
  total_price: number;

  @Column('decimal', { nullable: true })
  total_profit_share: number;

  @Column('decimal', { nullable: true })
  total_share_tenant: number;

  @Column('decimal', { nullable: true })
  payment_total_dpp: number;

  @Column('decimal', { nullable: true })
  payment_total_tax: number;

  @ManyToOne(() => TransactionItemModel, (model) => model.bundling_items, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'transaction_item_id' })
  transaction_item: TransactionItemModel;

  @OneToMany(() => TransactionBreakdownTaxModel, (model) => model.transaction, {
    cascade: true,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  item_taxes: TransactionBreakdownTaxModel[];
}

@Entity(TABLE_NAME.TRANSACTION_ITEM_TAX)
export class TransactionItemTaxModel
  extends BaseCoreModel<TransactionTaxEntity>
  implements TransactionTaxEntity
{
  @Column('varchar', { name: 'tax_id', nullable: true })
  tax_id: string;

  @Column('varchar', { name: 'tax_name', nullable: true })
  tax_name: string;

  @Column('decimal', { name: 'taxt_value', nullable: true })
  taxt_value: number;

  @Column('decimal', { name: 'tax_total_value', nullable: true })
  tax_total_value: number;

  @Column('varchar', { name: 'transaction_id', nullable: true })
  transaction_id: string;
  @ManyToOne(() => TransactionItemModel, (model) => model.taxes, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'transaction_id' })
  transaction: TransactionItemModel;
}

@Entity(TABLE_NAME.TRANSACTION_ITEM_BREAKDOWN_TAX)
export class TransactionBreakdownTaxModel
  extends BaseCoreModel<TransactionTaxEntity>
  implements TransactionTaxEntity
{
  @Column('varchar', { name: 'tax_id', nullable: true })
  tax_id: string;

  @Column('varchar', { name: 'tax_name', nullable: true })
  tax_name: string;

  @Column('decimal', { name: 'taxt_value', nullable: true })
  taxt_value: number;

  @Column('decimal', { name: 'tax_total_value', nullable: true })
  tax_total_value: number;

  @Column('varchar', { name: 'transaction_id', nullable: true })
  transaction_id: string;
  @ManyToOne(() => TransactionItemBreakdownModel, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'transaction_id' })
  transaction: TransactionItemBreakdownModel;
}
