import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, Index, ManyToOne, JoinColumn, OneToOne } from 'typeorm';
import { Seller } from './seller.entity';

@Entity('merchants')
export class Merchant {
  @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
  id: number;

  @OneToOne(() => Seller)
  @JoinColumn({ name: 'seller_id' })
  seller: Seller;

  @Column({ name: 'seller_id', type: 'bigint', unsigned: true, unique: true, comment: '关联商家账户ID' })
  sellerId: number;

  @Column({ name: 'shop_name', length: 100, comment: '店铺名称' })
  shopName: string;

  @Column({ length: 500, default: '', comment: '店铺Logo' })
  logo: string;

  @Column({ name: 'cover_image', length: 500, default: '', comment: '店铺封面图片' })
  coverImage: string;

  @Column({ name: 'hotel_images', type: 'text', nullable: true, comment: '酒店照片，多张URL用逗号分隔' })
  hotelImages: string;

  @Column({ type: 'text', nullable: true, comment: '店铺描述' })
  description: string;

  @Column({ name: 'store_license', length: 255, nullable: true, comment: '门店营业执照' })
  storeLicense: string;

  @Column({ length: 20, comment: '联系电话' })
  phone: string;

  @Column({ length: 50, default: '', comment: '省' })
  province: string;

  @Column({ length: 50, default: '', comment: '市' })
  city: string;

  @Column({ length: 50, default: '', comment: '区' })
  district: string;

  @Column({ length: 255, default: '', comment: '详细地址' })
  address: string;

  @Column({ type: 'decimal', precision: 10, scale: 7, nullable: true, comment: '经度' })
  longitude: number;

  @Column({ type: 'decimal', precision: 10, scale: 7, nullable: true, comment: '纬度' })
  latitude: number;

  @Column({ name: 'business_license', length: 500, default: '', comment: '营业执照图片' })
  businessLicense: string;

  @Index()
  @Column({ name: 'contract_type', length: 20, default: 'personal', comment: '签约类型：personal-个人签约，company-公司签约' })
  contractType: string;

  @Column({ name: 'id_card_front', length: 255, nullable: true, comment: '身份证正面（个人签约）' })
  idCardFront: string;

  @Column({ name: 'id_card_back', length: 255, nullable: true, comment: '身份证反面（个人签约）' })
  idCardBack: string;

  @Column({ name: 'legal_id_card_front', length: 255, nullable: true, comment: '法人身份证正面（公司签约）' })
  legalIdCardFront: string;

  @Column({ name: 'legal_id_card_back', length: 255, nullable: true, comment: '法人身份证反面（公司签约）' })
  legalIdCardBack: string;

  @Column({ name: 'license_no', length: 50, default: '', comment: '营业执照编号' })
  licenseNo: string;

  @Column({ name: 'legal_person', length: 50, default: '', comment: '法人姓名' })
  legalPerson: string;

  @Index()
  @Column({ type: 'enum', enum: ['pending', 'approved', 'rejected', 'frozen'], default: 'pending', comment: '状态' })
  status: 'pending' | 'approved' | 'rejected' | 'frozen';

  @Column({ name: 'reject_reason', length: 500, nullable: true, comment: '拒绝原因' })
  rejectReason?: string;

  @Column({ type: 'decimal', precision: 10, scale: 2, unsigned: true, default: 0, comment: '保证金' })
  deposit: number;

  @Column({ name: 'wallet_balance', type: 'decimal', precision: 10, scale: 2, unsigned: true, default: 0, comment: '待提现余额' })
  walletBalance: number;

  @Index()
  @Column({ name: 'account_type', length: 20, default: 'company', comment: '账户类型：company-对公账户，personal-对私账户' })
  accountType: string;

  @Column({ name: 'bank_name', length: 100, default: '', comment: '开户银行' })
  bankName: string;

  @Column({ name: 'bank_account', length: 50, default: '', comment: '银行账号' })
  bankAccount: string;

  @Column({ name: 'account_name', length: 50, default: '', comment: '账户名' })
  accountName: string;

  @Column({ name: 'bank_branch', length: 100, nullable: true, comment: '支行信息（对私账户）' })
  bankBranch: string;

  @Column({ name: 'bank_license', length: 255, nullable: true, comment: '开户营业执照（对公账户）' })
  bankLicense: string;

  @Column({ name: 'account_id_card_front', length: 255, nullable: true, comment: '开户人身份证正面（对私账户）' })
  accountIdCardFront: string;

  @Column({ name: 'account_id_card_back', length: 255, nullable: true, comment: '开户人身份证反面（对私账户）' })
  accountIdCardBack: string;

  @Index()
  @Column({
    type: 'decimal',
    precision: 2,
    scale: 1,
    unsigned: true,
    default: 5.0,
    comment: '评分',
    transformer: {
      to: (value: number) => value,
      from: (value: string) => value ? parseFloat(value) : 5.0
    }
  })
  rating: number;

  @Column({ name: 'review_count', type: 'int', unsigned: true, default: 0, comment: '评价数' })
  reviewCount: number;

  @Index()
  @Column({ name: 'sales_count', type: 'int', unsigned: true, default: 0, comment: '销量统计' })
  salesCount: number;

  @Column({ name: 'auto_confirm', type: 'boolean', default: false, comment: '是否自动接单' })
  autoConfirm: boolean;

  @Column({ name: 'auto_confirm_rules', type: 'json', nullable: true, comment: '自动接单规则配置' })
  autoConfirmRules: object;

  @CreateDateColumn({ name: 'created_at', comment: '创建时间' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', comment: '更新时间' })
  updatedAt: Date;
}
