import type { Request, Response } from 'express';
import type {
  VehicleItem,
  VehicleListParams,
  VehicleStatus,
  VehicleType,
} from '@/pages/VehicleManagement/data';

const now = () => new Date().toISOString();

const parseString = (value: unknown): string | undefined => {
  return typeof value === 'string' && value.trim() ? value.trim() : undefined;
};

const VEHICLE_STATUS_LIST: VehicleStatus[] = [
  'active',
  'inactive',
  'maintenance',
  'retired',
];

const VEHICLE_TYPE_LIST: VehicleType[] = ['car', 'truck', 'van', 'bus', 'other'];

const isVehicleStatus = (value: unknown): value is VehicleStatus => {
  return (
    typeof value === 'string' &&
    VEHICLE_STATUS_LIST.includes(value as VehicleStatus)
  );
};

const isVehicleType = (value: unknown): value is VehicleType => {
  return (
    typeof value === 'string' && VEHICLE_TYPE_LIST.includes(value as VehicleType)
  );
};

let vehicleList: VehicleItem[] = [
  {
    id: 'vh-1001',
    plateNumber: '沪A·12345',
    brand: 'Tesla',
    model: 'Model 3',
    type: 'car',
    status: 'active',
    vin: '5YJ3E1EA7HF000001',
    ownerName: '张三',
    remark: '日常城市运营',
    createdAt: '2024-02-14T10:18:00.000Z',
    updatedAt: '2024-02-14T10:18:00.000Z',
  },
  {
    id: 'vh-1002',
    plateNumber: '沪B·67890',
    brand: 'BYD',
    model: '唐 DM-i',
    type: 'car',
    status: 'maintenance',
    ownerName: '李四',
    remark: '电池系统检查中',
    createdAt: '2024-02-14T11:05:00.000Z',
    updatedAt: '2024-02-14T11:30:00.000Z',
  },
  {
    id: 'vh-1003',
    plateNumber: '沪C·00001',
    brand: 'Mercedes-Benz',
    model: 'Sprinter',
    type: 'van',
    status: 'inactive',
    ownerName: '王五',
    remark: '备用车辆',
    createdAt: '2024-02-15T09:20:00.000Z',
    updatedAt: '2024-02-15T09:20:00.000Z',
  },
];

const getQueryParams = (req: Request): VehicleListParams => {
  const { plateNumber, brand, status, type, current, pageSize } = req.query;

  return {
    plateNumber: parseString(plateNumber),
    brand: parseString(brand),
    status: isVehicleStatus(status) ? status : undefined,
    type: isVehicleType(type) ? type : undefined,
    current: Number(current) || 1,
    pageSize: Number(pageSize) || 10,
  };
};

export default {
  'GET /api/vehicles': (req: Request, res: Response) => {
    const { plateNumber, brand, status, type, current = 1, pageSize = 10 } =
      getQueryParams(req);

    let filtered = [...vehicleList];

    if (plateNumber) {
      filtered = filtered.filter((item) =>
        item.plateNumber.includes(plateNumber),
      );
    }

    if (brand) {
      filtered = filtered.filter((item) => item.brand.includes(brand));
    }

    if (status) {
      filtered = filtered.filter((item) => item.status === status);
    }

    if (type) {
      filtered = filtered.filter((item) => item.type === type);
    }

    const start = (current - 1) * pageSize;
    const data = filtered.slice(start, start + pageSize);

    res.json({
      success: true,
      data,
      total: filtered.length,
    });
  },

  'GET /api/vehicles/:id': (req: Request, res: Response) => {
    const { id } = req.params;
    const item = vehicleList.find((vehicle) => vehicle.id === id);

    if (!item) {
      res.status(404).json({ success: false, message: '车辆不存在' });
      return;
    }

    res.json({ success: true, data: item });
  },

  'POST /api/vehicles': (req: Request, res: Response) => {
    const body = req.body as Partial<VehicleItem>;
    const id = `vh-${Math.floor(Math.random() * 9000 + 1000)}`;
    const timestamp = now();

    const newItem: VehicleItem = {
      id,
      plateNumber: body.plateNumber || '',
      brand: body.brand || '',
      model: body.model || '',
      type: (body.type as VehicleType) || 'car',
      status: (body.status as VehicleStatus) || 'active',
      vin: body.vin,
      ownerName: body.ownerName,
      remark: body.remark,
      createdAt: timestamp,
      updatedAt: timestamp,
    };

    vehicleList.unshift(newItem);

    res.json({ success: true, data: newItem });
  },

  'PUT /api/vehicles/:id': (req: Request, res: Response) => {
    const { id } = req.params;
    const body = req.body as Partial<VehicleItem>;
    const index = vehicleList.findIndex((vehicle) => vehicle.id === id);

    if (index === -1) {
      res.status(404).json({ success: false, message: '车辆不存在' });
      return;
    }

    const prev = vehicleList[index];
    const updated: VehicleItem = {
      ...prev,
      ...body,
      type: isVehicleType(body.type) ? body.type : prev.type,
      status: isVehicleStatus(body.status) ? body.status : prev.status,
      updatedAt: now(),
    };

    vehicleList[index] = updated;

    res.json({ success: true, data: updated });
  },

  'PUT /api/vehicles/:id/status': (req: Request, res: Response) => {
    const { id } = req.params;
    const status = (req.body as any)?.status as unknown;
    const item = vehicleList.find((vehicle) => vehicle.id === id);

    if (!item) {
      res.status(404).json({ success: false, message: '车辆不存在' });
      return;
    }

    if (!isVehicleStatus(status)) {
      res.status(400).json({ success: false, message: '状态非法' });
      return;
    }

    item.status = status;
    item.updatedAt = now();

    res.json({ success: true });
  },

  'DELETE /api/vehicles/:id': (req: Request, res: Response) => {
    const { id } = req.params;
    const index = vehicleList.findIndex((vehicle) => vehicle.id === id);

    if (index === -1) {
      res.status(404).json({ success: false, message: '车辆不存在' });
      return;
    }

    vehicleList.splice(index, 1);
    res.json({ success: true });
  },
};

