import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';
import { ActivityEntity } from '../activities/activity.entity';
import { LocationEntity } from '../locations/location.entity';
import { DeleteStatus, DisabledStatus } from '../../share/types';
import { VehicleType, TravelMode } from './route.schema';

@Entity({ name: 'routes' })
export class RouteEntity {
  @PrimaryGeneratedColumn({ unsigned: true, comment: '路线ID' })
  id!: number;

  @Column({ name: 'name', type: 'varchar', length: 255, comment: '路线名称' })
  name!: string;

  @Column({ name: 'description', type: 'text', nullable: true, comment: '路线描述' })
  description!: string | null;

  @Column({ name: 'start_location_id', type: 'int', unsigned: true, comment: '出发地点ID' })
  startLocationId!: number;

  @Column({ name: 'end_location_id', type: 'int', unsigned: true, comment: '目的地点ID' })
  endLocationId!: number;

  @Column({ name: 'pickup_point', type: 'varchar', length: 255, comment: '上车点' })
  pickupPoint!: string;

  @Column({ name: 'dropoff_point', type: 'varchar', length: 255, comment: '下车点' })
  dropoffPoint!: string;

  @Column({ name: 'departure_time', type: 'timestamp', comment: '出发时间' })
  departureTime!: Date;

  @Column({ name: 'vehicle_type', type: 'enum', enum: VehicleType, comment: '车型' })
  vehicleType!: VehicleType;

  @Column({ name: 'travel_mode', type: 'enum', enum: TravelMode, comment: '出行方式', default: TravelMode.CARPOOL })
  travelMode!: TravelMode;

  @Column({ name: 'price', type: 'decimal', precision: 10, scale: 2, comment: '价格' })
  price!: number;

  @Column({ name: 'seat_count', type: 'int', unsigned: true, comment: '座位数' })
  seatCount!: number;

  @Column({ name: 'available_seats', type: 'int', unsigned: true, comment: '可用座位数' })
  availableSeats!: number;

  @Column({ name: 'activity_id', type: 'int', unsigned: true, comment: '关联活动ID' })
  activityId!: number;

  @ManyToOne(() => ActivityEntity, (activity) => activity.routes)
  @JoinColumn({ name: 'activity_id', referencedColumnName: 'id' })
  activity!: ActivityEntity;

  @ManyToOne(() => LocationEntity, { nullable: false })
  @JoinColumn({ name: 'start_location_id', referencedColumnName: 'id' })
  startLocation!: LocationEntity;

  @ManyToOne(() => LocationEntity, { nullable: false })
  @JoinColumn({ name: 'end_location_id', referencedColumnName: 'id' })
  endLocation!: LocationEntity;

  @Column({ name: 'is_disabled', type: 'int', default: DisabledStatus.ENABLED, comment: '是否禁用(0:启用,1:禁用)' })
  isDisabled!: DisabledStatus;

  @Column({ name: 'is_deleted', type: 'int', default: DeleteStatus.NOT_DELETED, comment: '是否删除(0:未删除,1:已删除)' })
  isDeleted!: DeleteStatus;

  @Column({ name: 'created_by', type: 'int', nullable: true, comment: '创建人ID' })
  createdBy!: number | null;

  @Column({ name: 'updated_by', type: 'int', nullable: true, comment: '更新人ID' })
  updatedBy!: number | null;

  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp' })
  updatedAt!: Date;

  constructor(partial?: Partial<RouteEntity>) {
    Object.assign(this, partial);
  }

  /**
   * 计算路线类型（去程/返程）
   * 去程路线: 目的地 = 活动举办地点
   * 返程路线: 出发地 = 活动举办地点
   */
  get routeType(): 'departure' | 'return' {
    if (!this.activity || !this.activity.venueLocationId) {
      return 'departure'; // 默认返回去程类型
    }

    if (this.endLocationId === this.activity.venueLocationId) {
      return 'departure';
    } else if (this.startLocationId === this.activity.venueLocationId) {
      return 'return';
    }

    return 'departure'; // 默认返回去程类型
  }
}