import {
  BaseEntity,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { BooleanTransformer } from '../../../utils/transformers/BooleanTransformer';
import { CategoryDiscountMappingEntity } from '../../category/entities/category-discount-mapping.entity';
import { OrderEntity } from '../../order/entities/order.entity';
import { CustomerEntity } from '../../customer/entities/customer.entity';
import { UserEntity } from '../../user/entities/user.entity';
import { PromotionPlanEntity } from '../../promotion-plan/entities/promotion-plan.entity';

export enum AvailableSection {
  product = 'product',
  service = 'service',
  both = 'both',
}

@Index('groupId', ['groupId'], {})
@Entity('discounts')
export class DiscountEntity extends BaseEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @Column('varchar', { name: 'name', nullable: true, length: 255 })
  name: string | null;

  @Column('char', { name: 'userId', nullable: true, length: 36 })
  userId: string | null;

  @Column('int', { name: 'customerId', nullable: true })
  customerId: number | null;

  @Column('int', { name: 'promotionPlanId', nullable: true })
  promotionPlanId: number | null;

  @Column('tinyint', {
    name: 'usePercentage',
    nullable: true,
    width: 1,
    transformer: new BooleanTransformer(),
  })
  usePercentage: boolean | null;

  @Column('int', { name: 'discountPercentage', nullable: true })
  discountPercentage: number | null;

  @Column('int', { name: 'discountAmount', nullable: true })
  discountAmount: number | null;

  @Column('datetime', { name: 'startDate', nullable: true })
  startDate: Date | null;

  @Column('datetime', { name: 'endDate', nullable: true })
  endDate: Date | null;

  @Column('tinyint', {
    name: 'requiresCouponCode',
    nullable: true,
    width: 1,
    transformer: new BooleanTransformer(),
  })
  requiresCouponCode: boolean | null;

  @Column('varchar', { name: 'couponCode', nullable: true, length: 10 })
  couponCode: string | null;

  @Column('int', { name: 'groupId', nullable: true })
  groupId: number | null;

  @Column('enum', {
    name: 'section',
    enum: AvailableSection,
    default: () => AvailableSection.service,
  })
  section: AvailableSection;

  @Column('tinyint', {
    name: 'deleted',
    nullable: true,
    width: 1,
    default: () => "'0'",
    transformer: new BooleanTransformer(),
  })
  deleted: boolean | null;

  @Column('datetime', { name: 'createdAt' })
  createdAt: Date;

  @Column('datetime', { name: 'updatedAt' })
  updatedAt: Date;

  /**
   * Relations
   */
  @OneToOne(() => OrderEntity, (order) => order.coupon)
  order: OrderEntity;

  @OneToMany(
    () => CategoryDiscountMappingEntity,
    (categoryDiscountMappings) => categoryDiscountMappings.discount,
  )
  categoryDiscountMappings: CategoryDiscountMappingEntity[];

  @ManyToOne(() => CustomerEntity, (customers) => customers.discounts, {
    onDelete: 'NO ACTION',
    onUpdate: 'NO ACTION',
  })
  @JoinColumn([{ name: 'customerId', referencedColumnName: 'id' }])
  customer: CustomerEntity;

  @ManyToOne(() => UserEntity, (users) => users.discounts, {
    onDelete: 'NO ACTION',
    onUpdate: 'NO ACTION',
  })
  @JoinColumn([{ name: 'userId', referencedColumnName: 'id' }])
  user: UserEntity;

  @ManyToOne(
    () => PromotionPlanEntity,
    (promotionPlans) => promotionPlans.discounts,
    { onDelete: 'NO ACTION', onUpdate: 'NO ACTION' },
  )
  @JoinColumn([{ name: 'promotionPlanId', referencedColumnName: 'id' }])
  promotionPlan: PromotionPlanEntity;

  // @OneToMany(
  //   () => DiscountProductMappings,
  //   (discountProductMappings) => discountProductMappings.discount,
  // )
  // discountProductMappings: DiscountProductMappings[];

  // @ManyToOne(() => Usergroups, (usergroups) => usergroups.discounts, {
  //   onDelete: 'NO ACTION',
  //   onUpdate: 'CASCADE',
  // })
  // @JoinColumn([{ name: 'groupId', referencedColumnName: 'id' }])
  // group: Usergroups;
}
