'use client';

import { useState, use } from 'react';
import Link from 'next/link';
import {
  Battery, ArrowLeft, X, BatteryCharging, Activity, Gauge, MapPin, Bike, User, History,
  Calendar, DollarSign, CheckCircle, Clock, ArrowRightLeft, Handshake, TrendingUp, Edit,
  RefreshCw, AlertTriangle, Wrench, Plus, Trash2, Search, ArrowUpDown, ChevronLeft,
  ChevronRight, CheckCircle2, XCircle, AlertCircle
} from 'lucide-react';

interface BMSData {
  voltage: number;
  current: number;
  soc: number;
  temperature: number;
  cycles: number;
  health: number;
  timestamp: string;
}

interface OwnershipLog {
  id: string;
  batteryId: string;
  fromOwner: string;
  fromOwnerType: 'company' | 'biker' | 'hub' | 'swap-station';
  toOwner: string;
  toOwnerType: 'company' | 'biker' | 'hub' | 'swap-station';
  fromBikeId?: string;
  fromBikeModel?: string;
  toBikeId?: string;
  toBikeModel?: string;
  fromHubId?: string;
  fromHubName?: string;
  toHubId?: string;
  toHubName?: string;
  fromStationId?: string;
  fromStationName?: string;
  toStationId?: string;
  toStationName?: string;
  action: 'assigned' | 'rented' | 'swapped' | 'returned' | 'retired';
  rentAmount?: number;
  notes?: string;
  timestamp: string;
}

interface DamageRecord {
  id: string;
  date: string;
  type: string;
  description: string;
  reportedBy: string;
  estimatedCost: number;
  actualCost?: number;
  status: 'reported' | 'in-progress' | 'resolved';
  hubId?: string;
  hubName?: string;
}

interface MaintenanceRecord {
  id: string;
  date: string;
  type: 'routine' | 'repair' | 'replacement' | 'inspection';
  description: string;
  cost: number;
  performedBy: string;
  nextDueDate?: string;
  status: 'completed' | 'pending' | 'overdue';
  hubId?: string;
  hubName?: string;
}

interface Battery {
  id: string;
  serialNumber: string;
  brand: string;
  model: string;
  type: 'lithium-ion' | 'lifepo4' | 'lead-acid';
  capacity: number;
  voltage: number;
  purchaseDate: string;
  purchasePrice: number;
  warrantyExpiry: string;
  status: 'available' | 'in-use' | 'maintenance' | 'retired' | 'charging';
  currentSoc: number;
  health: number;
  cycleCount: number;
  assignedBikeId?: string;
  assignedBikeModel?: string;
  assignedBikePlate?: string;
  assignedBikerId?: string;
  assignedBikerName?: string;
  assignedBikerPhone?: string;
  currentHubId?: string;
  currentHubName?: string;
  currentStationId?: string;
  currentStationName?: string;
  lastMaintenance?: string;
  nextMaintenance?: string;
  bmsData?: BMSData;
  deposit?: number;
  rentPrice?: number;
  investorId?: string;
  investorName?: string;
  investorSharePercentage?: number;
  investedAmount?: number;
  ownershipLogs: OwnershipLog[];
  damageHistory?: DamageRecord[];
  maintenanceHistory?: MaintenanceRecord[];
}

const mockBattery: Battery = {
  id: 'BAT-001',
  serialNumber: 'SN-2024-00001',
  brand: 'EVE Energy',
  model: 'Li-Ion 60V50Ah',
  type: 'lithium-ion',
  capacity: 50,
  voltage: 60,
  purchaseDate: '2024-01-15',
  purchasePrice: 45000,
  warrantyExpiry: '2027-01-15',
  status: 'in-use',
  currentSoc: 78,
  health: 95,
  cycleCount: 156,
  assignedBikeId: 'EV001',
  assignedBikeModel: 'Etron ET50',
  assignedBikePlate: 'Dhaka Metro Cha-A-1234',
  assignedBikerId: 'BK-001',
  assignedBikerName: 'Rahim Ahmed',
  assignedBikerPhone: '01712345678',
  currentHubId: 'HUB-001',
  currentHubName: 'JAIBEN Head Office',
  deposit: 5000,
  rentPrice: 150,
  investorId: 'INV-001',
  investorName: 'Tahmid Rahman',
  investorSharePercentage: 60,
  investedAmount: 45000,
  lastMaintenance: '2024-03-01',
  nextMaintenance: '2024-06-01',
  bmsData: { voltage: 67.2, current: -2.5, soc: 78, temperature: 32, cycles: 156, health: 95, timestamp: '2024-03-28 12:00:00' },
  ownershipLogs: [
    { id: 'OL-001', batteryId: 'BAT-001', fromOwner: 'JAIBEN Hub', fromOwnerType: 'hub', toOwner: 'Rahim Ahmed', toOwnerType: 'biker', toBikeId: 'EV001', toBikeModel: 'Etron ET50', toHubId: 'HUB-001', toHubName: 'JAIBEN Head Office', action: 'rented', rentAmount: 1500, timestamp: '2024-03-15 08:30:00' },
    { id: 'OL-002', batteryId: 'BAT-001', fromOwner: 'Rahim Ahmed', fromOwnerType: 'biker', toOwner: 'JAIBEN Hub', toOwnerType: 'hub', fromBikeId: 'EV001', fromBikeModel: 'Etron ET50', fromHubId: 'HUB-001', fromHubName: 'JAIBEN Head Office', action: 'returned', timestamp: '2024-03-28 18:00:00', notes: 'Returned for charging' },
    { id: 'OL-003', batteryId: 'BAT-001', fromOwner: 'JAIBEN Hub', fromOwnerType: 'hub', toOwner: 'Rahim Ahmed', toOwnerType: 'biker', toBikeId: 'EV001', toBikeModel: 'Etron ET50', toHubId: 'HUB-001', toHubName: 'JAIBEN Head Office', action: 'rented', rentAmount: 1500, timestamp: '2024-03-28 18:30:00' },
  ],
  damageHistory: [
    { id: 'DMG-001', date: '2024-02-15', type: 'Physical Damage', description: 'Battery casing cracked due to drop', reportedBy: 'Rahim Ahmed', estimatedCost: 5000, actualCost: 4500, status: 'resolved', hubId: 'HUB-001', hubName: 'Gulshan Hub' },
    { id: 'DMG-002', date: '2024-03-20', type: 'Connector Damage', description: 'Charging connector bent', reportedBy: 'Tech Staff', estimatedCost: 1500, status: 'in-progress', hubId: 'HUB-002', hubName: 'Banani Hub' },
  ],
  maintenanceHistory: [
    { id: 'MNT-001', date: '2024-01-20', type: 'routine', description: 'General inspection and cleaning', cost: 500, performedBy: 'Tech Team', nextDueDate: '2024-04-20', status: 'completed', hubId: 'HUB-001', hubName: 'Gulshan Hub' },
    { id: 'MNT-002', date: '2024-03-01', type: 'inspection', description: 'BMS calibration and cell balancing', cost: 800, performedBy: 'Engineer Team', nextDueDate: '2024-06-01', status: 'completed', hubId: 'HUB-003', hubName: 'Uttara Hub' },
    { id: 'MNT-003', date: '2024-04-15', type: 'repair', description: 'Replaced faulty connector', cost: 1500, performedBy: 'Tech Team', status: 'pending', hubId: 'HUB-004', hubName: 'Mirpur Hub' },
  ],
};

const statusColors: Record<string, string> = {
  available: 'bg-green-100 text-green-700',
  'in-use': 'bg-blue-100 text-blue-700',
  maintenance: 'bg-amber-100 text-amber-700',
  retired: 'bg-slate-100 text-slate-500',
  charging: 'bg-purple-100 text-purple-700',
};

const typeLabels: Record<string, string> = {
  'lithium-ion': 'Lithium-Ion',
  'lifepo4': 'LiFePO4',
  'lead-acid': 'Lead Acid',
};

export default function BatteryDetailPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = use(params);
  const [battery, setBattery] = useState<Battery>(mockBattery);
  const [activeTab, setActiveTab] = useState<'info' | 'bms' | 'history' | 'rent' | 'damage' | 'maintenance'>('info');
  const [showEditModal, setShowEditModal] = useState(false);
  const [refreshing, setRefreshing] = useState(false);

  // Damage History State
  const [showDamageModal, setShowDamageModal] = useState(false);
  const [editingDamage, setEditingDamage] = useState<DamageRecord | null>(null);
  const [damageForm, setDamageForm] = useState({
    date: '',
    type: '',
    description: '',
    reportedBy: '',
    estimatedCost: 0,
    actualCost: 0,
    status: 'reported' as 'reported' | 'in-progress' | 'resolved',
    hubId: '',
    hubName: '',
  });

  // Maintenance State
  const [showMaintenanceModal, setShowMaintenanceModal] = useState(false);
  const [editingMaintenance, setEditingMaintenance] = useState<MaintenanceRecord | null>(null);
  const [maintenanceForm, setMaintenanceForm] = useState({
    date: '',
    type: 'routine' as 'routine' | 'repair' | 'replacement' | 'inspection',
    description: '',
    cost: 0,
    performedBy: '',
    nextDueDate: '',
    status: 'pending' as 'completed' | 'pending' | 'overdue',
    hubId: '',
    hubName: '',
  });

  const mockHubs = [
    { id: 'HUB-001', name: 'Gulshan Hub' },
    { id: 'HUB-002', name: 'Banani Hub' },
    { id: 'HUB-003', name: 'Uttara Hub' },
    { id: 'HUB-004', name: 'Mirpur Hub' },
  ];

  const damageTypes = ['Physical Damage', 'Connector Damage', 'Battery Cell Damage', 'Charging Port Damage', 'Water Damage', 'Software Issue', 'Other'];
  const maintenanceTypes = ['routine', 'repair', 'replacement', 'inspection'] as const;

  const handleSaveEdit = (updatedBattery: Battery) => {
    setBattery(updatedBattery);
    setShowEditModal(false);
  };

  const handleRefreshBMS = () => {
    if (!battery.bmsData) return;
    setRefreshing(true);
    setTimeout(() => {
      const bms = battery.bmsData!;
      setBattery({
        ...battery,
        bmsData: {
          ...bms,
          voltage: Math.round((60 + Math.random() * 10) * 10) / 10,
          current: Math.round((-3 + Math.random() * 4) * 10) / 10,
          soc: Math.floor(Math.random() * 40) + 60,
          temperature: 25 + Math.floor(Math.random() * 15),
          cycles: bms.cycles + 1,
          health: Math.max(70, Math.min(100, bms.health + (Math.random() > 0.7 ? 1 : 0))),
          timestamp: new Date().toISOString().replace('T', ' ').substring(0, 19)
        },
        currentSoc: Math.floor(Math.random() * 40) + 60,
        health: Math.max(70, Math.min(100, battery.health + (Math.random() > 0.7 ? 1 : 0)))
      });
      setRefreshing(false);
    }, 1000);
  };

  // Damage CRUD Handlers
  const handleAddDamage = () => {
    const newDamage: DamageRecord = {
      id: `DMG-${Date.now()}`,
      ...damageForm,
      estimatedCost: Number(damageForm.estimatedCost),
      actualCost: damageForm.actualCost ? Number(damageForm.actualCost) : undefined,
    };
    setBattery(prev => ({
      ...prev,
      damageHistory: [...(prev.damageHistory || []), newDamage]
    }));
    setShowDamageModal(false);
    resetDamageForm();
  };

  const handleUpdateDamage = () => {
    if (!editingDamage) return;
    const updatedDamage: DamageRecord = {
      ...editingDamage,
      ...damageForm,
      estimatedCost: Number(damageForm.estimatedCost),
      actualCost: damageForm.actualCost ? Number(damageForm.actualCost) : undefined,
    };
    setBattery(prev => ({
      ...prev,
      damageHistory: prev.damageHistory?.map(d => d.id === editingDamage.id ? updatedDamage : d) || []
    }));
    setShowDamageModal(false);
    setEditingDamage(null);
    resetDamageForm();
  };

  const handleDeleteDamage = (damageId: string) => {
    setBattery(prev => ({
      ...prev,
      damageHistory: prev.damageHistory?.filter(d => d.id !== damageId) || []
    }));
  };

  const resetDamageForm = () => {
    setDamageForm({
      date: '',
      type: '',
      description: '',
      reportedBy: '',
      estimatedCost: 0,
      actualCost: 0,
      status: 'reported',
      hubId: '',
      hubName: '',
    });
  };

  const openAddDamageModal = () => {
    resetDamageForm();
    setDamageForm(prev => ({ ...prev, date: new Date().toISOString().split('T')[0] }));
    setEditingDamage(null);
    setShowDamageModal(true);
  };

  const openEditDamageModal = (damage: DamageRecord) => {
    setDamageForm({
      date: damage.date,
      type: damage.type,
      description: damage.description,
      reportedBy: damage.reportedBy,
      estimatedCost: damage.estimatedCost,
      actualCost: damage.actualCost || 0,
      status: damage.status,
      hubId: damage.hubId || '',
      hubName: damage.hubName || '',
    });
    setEditingDamage(damage);
    setShowDamageModal(true);
  };

  // Maintenance CRUD Handlers
  const handleAddMaintenance = () => {
    const newMaintenance: MaintenanceRecord = {
      id: `MNT-${Date.now()}`,
      ...maintenanceForm,
      cost: Number(maintenanceForm.cost),
      nextDueDate: maintenanceForm.nextDueDate || undefined,
    };
    setBattery(prev => ({
      ...prev,
      maintenanceHistory: [...(prev.maintenanceHistory || []), newMaintenance]
    }));
    setShowMaintenanceModal(false);
    resetMaintenanceForm();
  };

  const handleUpdateMaintenance = () => {
    if (!editingMaintenance) return;
    const updatedMaintenance: MaintenanceRecord = {
      ...editingMaintenance,
      ...maintenanceForm,
      cost: Number(maintenanceForm.cost),
      nextDueDate: maintenanceForm.nextDueDate || undefined,
    };
    setBattery(prev => ({
      ...prev,
      maintenanceHistory: prev.maintenanceHistory?.map(m => m.id === editingMaintenance.id ? updatedMaintenance : m) || []
    }));
    setShowMaintenanceModal(false);
    setEditingMaintenance(null);
    resetMaintenanceForm();
  };

  const handleDeleteMaintenance = (maintenanceId: string) => {
    setBattery(prev => ({
      ...prev,
      maintenanceHistory: prev.maintenanceHistory?.filter(m => m.id !== maintenanceId) || []
    }));
  };

  const resetMaintenanceForm = () => {
    setMaintenanceForm({
      date: '',
      type: 'routine',
      description: '',
      cost: 0,
      performedBy: '',
      nextDueDate: '',
      status: 'pending',
      hubId: '',
      hubName: '',
    });
  };

  const openAddMaintenanceModal = () => {
    resetMaintenanceForm();
    setMaintenanceForm(prev => ({ ...prev, date: new Date().toISOString().split('T')[0] }));
    setEditingMaintenance(null);
    setShowMaintenanceModal(true);
  };

  const openEditMaintenanceModal = (maintenance: MaintenanceRecord) => {
    setMaintenanceForm({
      date: maintenance.date,
      type: maintenance.type,
      description: maintenance.description,
      cost: maintenance.cost,
      performedBy: maintenance.performedBy,
      nextDueDate: maintenance.nextDueDate || '',
      status: maintenance.status,
      hubId: maintenance.hubId || '',
      hubName: maintenance.hubName || '',
    });
    setEditingMaintenance(maintenance);
    setShowMaintenanceModal(true);
  };

  return (
    <div className="p-4 lg:p-6">
      <div className="flex items-center gap-4 mb-6">
        <Link href="/admin/batteries" className="p-2 hover:bg-slate-100 rounded-lg">
          <ArrowLeft className="w-5 h-5 text-slate-600" />
        </Link>
        <div className="flex-1">
          <h1 className="text-2xl lg:text-3xl font-extrabold text-slate-800">Battery Details</h1>
          <p className="text-sm text-slate-500">ID: {battery.id}</p>
        </div>
        <button
          onClick={() => setShowEditModal(true)}
          className="py-2 px-4 bg-accent text-white rounded-lg font-semibold text-sm hover:bg-accent-dark flex items-center gap-2"
        >
          <Edit className="w-4 h-4" /> Edit
        </button>
      </div>



      <div className="bg-white rounded-xl shadow-sm border border-slate-100 mb-6">
        <div className="p-5 border-b border-slate-100">
          <div className="flex flex-col lg:flex-row lg:items-center gap-4">
            <div className="flex items-center gap-4">
              <div className={`w-14 h-14 rounded-xl flex items-center justify-center ${battery.type === 'lithium-ion' ? 'bg-blue-50' : battery.type === 'lifepo4' ? 'bg-green-50' : 'bg-amber-50'}`}>
                <Battery className={`w-7 h-7 ${battery.type === 'lithium-ion' ? 'text-blue-600' : battery.type === 'lifepo4' ? 'text-green-600' : 'text-amber-600'}`} />
              </div>
              <div>
                <h2 className="text-lg font-bold text-slate-800">{battery.brand} {battery.model}</h2>
                <p className="text-sm text-slate-500">{battery.serialNumber}</p>
                <span className={`inline-flex items-center gap-1 text-xs font-medium px-2 py-0.5 rounded-full mt-1 ${statusColors[battery.status]}`}>
                  {battery.status}
                </span>
              </div>
            </div>

            <div className="flex-1 grid grid-cols-2 md:grid-cols-4 gap-3 lg:ml-auto">
              <div className="text-center">
                <p className="text-xs text-slate-500">SOC</p>
                <p className={`text-lg font-bold ${battery.currentSoc > 50 ? 'text-green-600' : battery.currentSoc > 20 ? 'text-amber-600' : 'text-red-600'}`}>{battery.currentSoc}%</p>
              </div>
              <div className="text-center">
                <p className="text-xs text-slate-500">Health</p>
                <p className={`text-lg font-bold ${battery.health > 80 ? 'text-green-600' : battery.health > 60 ? 'text-amber-600' : 'text-red-600'}`}>{battery.health}%</p>
              </div>
              <div className="text-center">
                <p className="text-xs text-slate-500">Cycles</p>
                <p className="text-lg font-bold text-slate-700">{battery.cycleCount}</p>
              </div>
              <div className="text-center">
                <p className="text-xs text-slate-500">Price</p>
                <p className="text-lg font-bold text-slate-700">৳{battery.purchasePrice.toLocaleString()}</p>
              </div>
            </div>
          </div>
        </div>

        {battery.bmsData && (
          <div className="p-5 border-b border-slate-100 bg-gradient-to-r from-green-50 to-emerald-50">
            <div className="flex items-center justify-between mb-3">
              <div className="flex items-center gap-2">
                <Activity className="w-4 h-4 text-green-600" />
                <span className="font-medium text-green-800">Live BMS Data</span>
                <span className="text-xs text-green-600 bg-green-100 px-2 py-0.5 rounded-full">Real-time</span>
              </div>
              <div className="flex items-center gap-2">
                <span className="text-xs text-green-600">Updated: {battery.bmsData.timestamp}</span>
                <button onClick={handleRefreshBMS} disabled={refreshing} className="p-1.5 text-green-600 hover:bg-green-100 rounded-lg">
                  <RefreshCw className={`w-4 h-4 ${refreshing ? 'animate-spin' : ''}`} />
                </button>
              </div>
            </div>
            <div className="grid grid-cols-3 md:grid-cols-6 gap-2">
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Voltage</p>
                <p className="font-semibold text-slate-700">{battery.bmsData.voltage}V</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Current</p>
                <p className="font-semibold text-slate-700">{battery.bmsData.current}A</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">SOC</p>
                <p className="font-semibold text-green-600">{battery.bmsData.soc}%</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Temp</p>
                <p className="font-semibold text-slate-700">{battery.bmsData.temperature}°C</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Cycles</p>
                <p className="font-semibold text-slate-700">{battery.bmsData.cycles}</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Health</p>
                <p className={`font-semibold ${battery.bmsData.health > 80 ? 'text-green-600' : 'text-amber-600'}`}>{battery.bmsData.health}%</p>
              </div>
            </div>
          </div>
        )}

        <div className="border-b border-slate-100">
          <div className="flex gap-1 p-1 overflow-x-auto">
            <button onClick={() => setActiveTab('info')} className={`px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap ${activeTab === 'info' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>Info</button>
            <button onClick={() => setActiveTab('bms')} className={`px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap ${activeTab === 'bms' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>BMS</button>
            <button onClick={() => setActiveTab('history')} className={`px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap ${activeTab === 'history' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>History</button>
            <button onClick={() => setActiveTab('rent')} className={`px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap ${activeTab === 'rent' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>Rent</button>
            <button onClick={() => setActiveTab('damage')} className={`px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap ${activeTab === 'damage' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>Damage History</button>
            <button onClick={() => setActiveTab('maintenance')} className={`px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap ${activeTab === 'maintenance' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>Maintenance</button>
          </div>
        </div>

        <div className="p-5">
          {activeTab === 'info' && (
            <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Brand</p>
                <p className="font-medium text-slate-700">{battery.brand}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Model</p>
                <p className="font-medium text-slate-700">{battery.model}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Serial Number</p>
                <p className="font-medium text-slate-700">{battery.serialNumber}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Type</p>
                <p className="font-medium text-slate-700">{typeLabels[battery.type]}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Capacity</p>
                <p className="font-medium text-slate-700">{battery.capacity} Ah</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Voltage</p>
                <p className="font-medium text-slate-700">{battery.voltage} V</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Purchase Date</p>
                <p className="font-medium text-slate-700">{battery.purchaseDate}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Warranty Expiry</p>
                <p className="font-medium text-slate-700">{battery.warrantyExpiry}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Last Maintenance</p>
                <p className="font-medium text-slate-700">{battery.lastMaintenance || 'N/A'}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Next Maintenance</p>
                <p className="font-medium text-slate-700">{battery.nextMaintenance || 'N/A'}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Purchase Price</p>
                <p className="font-medium text-slate-700">৳{battery.purchasePrice.toLocaleString()}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Deposit</p>
                <p className="font-medium text-emerald-600">৳{(battery.deposit || 5000).toLocaleString()}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Daily Rent Price</p>
                <p className="font-medium text-blue-600">৳{(battery.rentPrice || 150).toLocaleString()}/day</p>
              </div>
            </div>
          )}

          {activeTab === 'bms' && battery.bmsData && (
            <div className="space-y-4">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2">
                  <Activity className="w-5 h-5 text-green-600" />
                  <h4 className="font-medium text-slate-700">BMS Real-time Data</h4>
                </div>
                <span className="text-xs text-slate-400">Updated: {battery.bmsData.timestamp}</span>
              </div>
              <div className="grid grid-cols-2 md:grid-cols-3 gap-4">
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Voltage</p>
                  <p className="text-2xl font-bold text-slate-700">{battery.bmsData.voltage}V</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Current</p>
                  <p className="text-2xl font-bold text-slate-700">{battery.bmsData.current}A</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">SOC</p>
                  <p className="text-2xl font-bold text-green-600">{battery.bmsData.soc}%</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Temperature</p>
                  <p className="text-2xl font-bold text-slate-700">{battery.bmsData.temperature}°C</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Cycles</p>
                  <p className="text-2xl font-bold text-slate-700">{battery.bmsData.cycles}</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Health</p>
                  <p className={`text-2xl font-bold ${battery.bmsData.health > 80 ? 'text-green-600' : 'text-amber-600'}`}>{battery.bmsData.health}%</p>
                </div>
              </div>
            </div>
          )}

          {activeTab === 'history' && (
            <div>
              <div className="flex items-center gap-2 mb-4">
                <History className="w-5 h-5 text-purple-600" />
                <h4 className="font-medium text-slate-700">Ownership History</h4>
              </div>
              {battery.ownershipLogs.length === 0 ? (
                <div className="text-center py-8 text-slate-400">No history records</div>
              ) : (
                <div className="space-y-3">
                  {battery.ownershipLogs.map((log, index) => (
                    <div key={log.id} className="relative pl-6 pb-4 border-l-2 border-slate-200 last:border-0">
                      <div className={`absolute -left-2 top-0 w-4 h-4 rounded-full ${log.action === 'rented' ? 'bg-green-500' : log.action === 'swapped' ? 'bg-blue-500' : log.action === 'returned' ? 'bg-amber-500' : 'bg-slate-400'}`} />
                      <div className="bg-slate-50 rounded-lg p-3">
                        <div className="flex items-center justify-between mb-2">
                          <span className={`text-xs font-medium px-2 py-0.5 rounded-full ${log.action === 'rented' ? 'bg-green-100 text-green-700' : log.action === 'swapped' ? 'bg-blue-100 text-blue-700' : log.action === 'returned' ? 'bg-amber-100 text-amber-700' : 'bg-slate-100 text-slate-500'}`}>
                            {log.action}
                          </span>
                          <span className="text-xs text-slate-400">{log.timestamp}</span>
                        </div>
                        <div className="grid grid-cols-2 gap-2 text-sm">
                          <div>
                            <p className="text-xs text-slate-500">From</p>
                            <p className="font-medium text-slate-700">{log.fromOwner}</p>
                          </div>
                          <div>
                            <p className="text-xs text-slate-500">To</p>
                            <p className="font-medium text-slate-700">{log.toOwner}</p>
                          </div>
                        </div>
                        {log.rentAmount && (
                          <p className="text-xs text-green-600 mt-2">Rent: ৳{log.rentAmount}/month</p>
                        )}
                        {log.notes && (
                          <p className="text-xs text-slate-400 mt-1 italic">{log.notes}</p>
                        )}
                      </div>
                    </div>
                  ))}
                </div>
              )}
            </div>
          )}

          {activeTab === 'rent' && (
            <div>
              <div className="flex items-center gap-2 mb-4">
                <DollarSign className="w-5 h-5 text-green-600" />
                <h4 className="font-medium text-slate-700">Rent Information</h4>
              </div>
              {battery.rentPrice ? (
                <>
                  <div className="bg-green-50 rounded-lg p-5 border border-green-100">
                    <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
                      <div>
                        <p className="text-sm text-blue-600 mb-1">Daily Rent Price</p>
                        <p className="text-2xl font-bold text-blue-700">৳{battery.rentPrice.toLocaleString()}</p>
                        <p className="text-xs text-blue-500 mt-1">per day</p>
                      </div>
                      <div>
                        <p className="text-sm text-emerald-600 mb-1">Battery Deposit</p>
                        <p className="text-2xl font-bold text-emerald-700">৳{(battery.deposit || 0).toLocaleString()}</p>
                        <p className="text-xs text-emerald-500 mt-1">refundable</p>
                      </div>
                      <div>
                        <p className="text-sm text-green-600 mb-1">Status</p>
                        <p className="text-lg font-semibold text-green-700">{battery.assignedBikerName ? 'Rented' : 'Not Rented'}</p>
                        <p className="text-xs text-green-500 mt-1">{battery.assignedBikerName ? `to ${battery.assignedBikerName}` : 'Available for rental'}</p>
                      </div>
                    </div>
                  </div>
                  <BatteryRentalTab battery={battery} />
                </>
              ) : (
                <div className="bg-slate-50 rounded-lg p-5 text-center text-slate-500">
                  This battery is not set up for rental.
                </div>
              )}
            </div>
          )}

          {activeTab === 'damage' && (
            <div className="space-y-4">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2">
                  <AlertTriangle className="w-5 h-5 text-red-500" />
                  <h4 className="font-medium text-slate-700">Damage History</h4>
                </div>
                <button onClick={openAddDamageModal} className="px-4 py-2 bg-accent text-white text-sm rounded-lg hover:bg-accent-dark flex items-center gap-2">
                  <Plus className="w-4 h-4" /> Add Damage
                </button>
              </div>
              {(battery.damageHistory || []).length > 0 ? (
                <div className="overflow-x-auto">
                  <table className="w-full">
                    <thead className="bg-slate-50">
                      <tr>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Date</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Type</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Description</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Hub</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Est. Cost</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Actual Cost</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Status</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Actions</th>
                      </tr>
                    </thead>
                    <tbody className="divide-y divide-slate-100">
                      {battery.damageHistory?.map(damage => (
                        <tr key={damage.id} className="hover:bg-slate-50">
                          <td className="px-4 py-3 text-sm text-slate-600">{damage.date}</td>
                          <td className="px-4 py-3 text-sm text-slate-700">{damage.type}</td>
                          <td className="px-4 py-3 text-sm text-slate-600">{damage.description}</td>
                          <td className="px-4 py-3 text-sm text-slate-600">{damage.hubName || '-'}</td>
                          <td className="px-4 py-3 text-sm font-medium text-amber-600">৳{damage.estimatedCost.toLocaleString()}</td>
                          <td className="px-4 py-3 text-sm font-medium text-green-600">{damage.actualCost ? `৳${damage.actualCost.toLocaleString()}` : '-'}</td>
                          <td className="px-4 py-3">
                            <span className={`text-xs font-medium px-2 py-1 rounded-full ${damage.status === 'resolved' ? 'bg-green-100 text-green-700' : damage.status === 'in-progress' ? 'bg-amber-100 text-amber-700' : 'bg-red-100 text-red-700'}`}>
                              {damage.status === 'resolved' ? 'Resolved' : damage.status === 'in-progress' ? 'In Progress' : 'Reported'}
                            </span>
                          </td>
                          <td className="px-4 py-3">
                            <div className="flex items-center gap-1">
                              <button onClick={() => openEditDamageModal(damage)} className="p-1.5 hover:bg-blue-100 rounded-lg">
                                <Edit className="w-4 h-4 text-blue-500" />
                              </button>
                              <button onClick={() => handleDeleteDamage(damage.id)} className="p-1.5 hover:bg-red-100 rounded-lg">
                                <Trash2 className="w-4 h-4 text-red-500" />
                              </button>
                            </div>
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              ) : (
                <div className="bg-slate-50 rounded-lg p-5 text-center text-slate-500">
                  No damage history found.
                </div>
              )}
            </div>
          )}

          {activeTab === 'maintenance' && (
            <div className="space-y-4">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2">
                  <Wrench className="w-5 h-5 text-blue-500" />
                  <h4 className="font-medium text-slate-700">Maintenance History</h4>
                </div>
                <button onClick={openAddMaintenanceModal} className="px-4 py-2 bg-accent text-white text-sm rounded-lg hover:bg-accent-dark flex items-center gap-2">
                  <Plus className="w-4 h-4" /> Add Maintenance
                </button>
              </div>
              {(battery.maintenanceHistory || []).length > 0 ? (
                <div className="overflow-x-auto">
                  <table className="w-full">
                    <thead className="bg-slate-50">
                      <tr>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Date</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Type</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Description</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Hub</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Cost</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Performed By</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Next Due</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Status</th>
                        <th className="px-4 py-3 text-left text-xs font-semibold text-slate-500 uppercase">Actions</th>
                      </tr>
                    </thead>
                    <tbody className="divide-y divide-slate-100">
                      {battery.maintenanceHistory?.map(maint => (
                        <tr key={maint.id} className="hover:bg-slate-50">
                          <td className="px-4 py-3 text-sm text-slate-600">{maint.date}</td>
                          <td className="px-4 py-3 text-sm text-slate-700 capitalize">{maint.type}</td>
                          <td className="px-4 py-3 text-sm text-slate-600">{maint.description}</td>
                          <td className="px-4 py-3 text-sm text-slate-600">{maint.hubName || '-'}</td>
                          <td className="px-4 py-3 text-sm font-medium text-green-600">৳{maint.cost.toLocaleString()}</td>
                          <td className="px-4 py-3 text-sm text-slate-600">{maint.performedBy}</td>
                          <td className="px-4 py-3 text-sm text-slate-600">{maint.nextDueDate || '-'}</td>
                          <td className="px-4 py-3">
                            <span className={`text-xs font-medium px-2 py-1 rounded-full ${maint.status === 'completed' ? 'bg-green-100 text-green-700' : maint.status === 'pending' ? 'bg-amber-100 text-amber-700' : 'bg-red-100 text-red-700'}`}>
                              {maint.status === 'completed' ? 'Completed' : maint.status === 'pending' ? 'Pending' : 'Overdue'}
                            </span>
                          </td>
                          <td className="px-4 py-3">
                            <div className="flex items-center gap-1">
                              <button onClick={() => openEditMaintenanceModal(maint)} className="p-1.5 hover:bg-blue-100 rounded-lg">
                                <Edit className="w-4 h-4 text-blue-500" />
                              </button>
                              <button onClick={() => handleDeleteMaintenance(maint.id)} className="p-1.5 hover:bg-red-100 rounded-lg">
                                <Trash2 className="w-4 h-4 text-red-500" />
                              </button>
                            </div>
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              ) : (
                <div className="bg-slate-50 rounded-lg p-5 text-center text-slate-500">
                  No maintenance history found.
                </div>
              )}
            </div>
          )}
        </div>
      </div>

      {/* Damage Modal */}
      {showDamageModal && (
        <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
          <div className="bg-white rounded-xl shadow-xl w-full max-w-lg max-h-[90vh] overflow-y-auto">
            <div className="p-4 border-b border-slate-100 flex justify-between items-center">
              <h3 className="font-semibold text-slate-800">{editingDamage ? 'Edit Damage' : 'Add Damage'}</h3>
              <button onClick={() => { setShowDamageModal(false); setEditingDamage(null); }} className="text-slate-400 hover:text-slate-600">
                <X className="w-5 h-5" />
              </button>
            </div>
            <div className="p-4 space-y-4">
              <div className="grid grid-cols-2 gap-4">
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Date</label>
                  <input type="date" value={damageForm.date} onChange={(e) => setDamageForm({ ...damageForm, date: e.target.value })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" />
                </div>
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Type</label>
                  <select value={damageForm.type} onChange={(e) => setDamageForm({ ...damageForm, type: e.target.value })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm">
                    <option value="">Select Type</option>
                    {damageTypes.map(type => (
                      <option key={type} value={type}>{type}</option>
                    ))}
                  </select>
                </div>
              </div>
              <div>
                <label className="text-sm font-medium text-slate-600 mb-1 block">Description</label>
                <textarea value={damageForm.description} onChange={(e) => setDamageForm({ ...damageForm, description: e.target.value })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" rows={2} />
              </div>
              <div className="grid grid-cols-2 gap-4">
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Reported By</label>
                  <input type="text" value={damageForm.reportedBy} onChange={(e) => setDamageForm({ ...damageForm, reportedBy: e.target.value })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" />
                </div>
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Hub</label>
                  <select value={damageForm.hubId} onChange={(e) => setDamageForm({ ...damageForm, hubId: e.target.value, hubName: mockHubs.find(h => h.id === e.target.value)?.name || '' })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm">
                    <option value="">Select Hub</option>
                    {mockHubs.map(hub => (
                      <option key={hub.id} value={hub.id}>{hub.name}</option>
                    ))}
                  </select>
                </div>
              </div>
              <div className="grid grid-cols-2 gap-4">
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Estimated Cost (৳)</label>
                  <input type="number" value={damageForm.estimatedCost} onChange={(e) => setDamageForm({ ...damageForm, estimatedCost: Number(e.target.value) })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" />
                </div>
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Actual Cost (৳)</label>
                  <input type="number" value={damageForm.actualCost} onChange={(e) => setDamageForm({ ...damageForm, actualCost: Number(e.target.value) })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" />
                </div>
              </div>
              <div>
                <label className="text-sm font-medium text-slate-600 mb-1 block">Status</label>
                <select value={damageForm.status} onChange={(e) => setDamageForm({ ...damageForm, status: e.target.value as any })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm">
                  <option value="reported">Reported</option>
                  <option value="in-progress">In Progress</option>
                  <option value="resolved">Resolved</option>
                </select>
              </div>
              <div className="flex gap-2 pt-4">
                <button onClick={() => { setShowDamageModal(false); setEditingDamage(null); }} className="flex-1 py-2 px-4 border border-slate-200 text-slate-600 rounded-lg text-sm hover:bg-slate-50">Cancel</button>
                <button onClick={editingDamage ? handleUpdateDamage : handleAddDamage} className="flex-1 py-2 px-4 bg-accent text-white rounded-lg text-sm hover:bg-accent-dark">{editingDamage ? 'Update' : 'Add'}</button>
              </div>
            </div>
          </div>
        </div>
      )}

      {/* Maintenance Modal */}
      {showMaintenanceModal && (
        <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
          <div className="bg-white rounded-xl shadow-xl w-full max-w-lg max-h-[90vh] overflow-y-auto">
            <div className="p-4 border-b border-slate-100 flex justify-between items-center">
              <h3 className="font-semibold text-slate-800">{editingMaintenance ? 'Edit Maintenance' : 'Add Maintenance'}</h3>
              <button onClick={() => { setShowMaintenanceModal(false); setEditingMaintenance(null); }} className="text-slate-400 hover:text-slate-600">
                <X className="w-5 h-5" />
              </button>
            </div>
            <div className="p-4 space-y-4">
              <div className="grid grid-cols-2 gap-4">
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Date</label>
                  <input type="date" value={maintenanceForm.date} onChange={(e) => setMaintenanceForm({ ...maintenanceForm, date: e.target.value })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" />
                </div>
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Type</label>
                  <select value={maintenanceForm.type} onChange={(e) => setMaintenanceForm({ ...maintenanceForm, type: e.target.value as any })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm">
                    {maintenanceTypes.map(type => (
                      <option key={type} value={type}>{type.charAt(0).toUpperCase() + type.slice(1)}</option>
                    ))}
                  </select>
                </div>
              </div>
              <div>
                <label className="text-sm font-medium text-slate-600 mb-1 block">Description</label>
                <textarea value={maintenanceForm.description} onChange={(e) => setMaintenanceForm({ ...maintenanceForm, description: e.target.value })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" rows={2} />
              </div>
              <div className="grid grid-cols-2 gap-4">
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Hub</label>
                  <select value={maintenanceForm.hubId} onChange={(e) => setMaintenanceForm({ ...maintenanceForm, hubId: e.target.value, hubName: mockHubs.find(h => h.id === e.target.value)?.name || '' })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm">
                    <option value="">Select Hub</option>
                    {mockHubs.map(hub => (
                      <option key={hub.id} value={hub.id}>{hub.name}</option>
                    ))}
                  </select>
                </div>
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Cost (৳)</label>
                  <input type="number" value={maintenanceForm.cost} onChange={(e) => setMaintenanceForm({ ...maintenanceForm, cost: Number(e.target.value) })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" />
                </div>
              </div>
              <div>
                <label className="text-sm font-medium text-slate-600 mb-1 block">Performed By</label>
                <input type="text" value={maintenanceForm.performedBy} onChange={(e) => setMaintenanceForm({ ...maintenanceForm, performedBy: e.target.value })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" />
              </div>
              <div className="grid grid-cols-2 gap-4">
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Next Due Date</label>
                  <input type="date" value={maintenanceForm.nextDueDate} onChange={(e) => setMaintenanceForm({ ...maintenanceForm, nextDueDate: e.target.value })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm" />
                </div>
                <div>
                  <label className="text-sm font-medium text-slate-600 mb-1 block">Status</label>
                  <select value={maintenanceForm.status} onChange={(e) => setMaintenanceForm({ ...maintenanceForm, status: e.target.value as any })} className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm">
                    <option value="completed">Completed</option>
                    <option value="pending">Pending</option>
                    <option value="overdue">Overdue</option>
                  </select>
                </div>
              </div>
              <div className="flex gap-2 pt-4">
                <button onClick={() => { setShowMaintenanceModal(false); setEditingMaintenance(null); }} className="flex-1 py-2 px-4 border border-slate-200 text-slate-600 rounded-lg text-sm hover:bg-slate-50">Cancel</button>
                <button onClick={editingMaintenance ? handleUpdateMaintenance : handleAddMaintenance} className="flex-1 py-2 px-4 bg-accent text-white rounded-lg text-sm hover:bg-accent-dark">{editingMaintenance ? 'Update' : 'Add'}</button>
              </div>
            </div>
          </div>
        </div>
      )}

      {battery.status === 'in-use' && (
        <div className="bg-blue-50 rounded-xl border border-blue-200 p-5 mb-6">
          <div className="flex items-center gap-3 mb-4">
            <div className="w-10 h-10 rounded-lg bg-blue-100 flex items-center justify-center">
              <User className="w-5 h-5 text-blue-600" />
            </div>
            <div>
              <h3 className="font-semibold text-blue-800">Currently Rented to Biker</h3>
              <span className="text-xs text-blue-600">Active Rental</span>
            </div>
          </div>

          <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
            <div className="bg-white rounded-lg p-3">
              <p className="text-xs text-blue-500 mb-1">Biker</p>
              <p className="font-medium text-blue-900">{battery.assignedBikerName}</p>
              <p className="text-xs text-blue-400">{battery.assignedBikerPhone}</p>
            </div>
            <div className="bg-white rounded-lg p-3">
              <p className="text-xs text-blue-500 mb-1">Bike</p>
              <p className="font-medium text-blue-900">{battery.assignedBikeModel}</p>
              <p className="text-xs text-blue-400">{battery.assignedBikePlate}</p>
            </div>
            <div className="bg-white rounded-lg p-3">
              <p className="text-xs text-blue-500 mb-1">Hub/Station</p>
              <p className="font-medium text-blue-900">{battery.currentHubName || battery.currentStationName || 'Not Assigned'}</p>
            </div>
            <div className="bg-white rounded-lg p-3">
              <p className="text-xs text-blue-500 mb-1">Daily Rent</p>
              <p className="font-medium text-green-600">৳{(battery.rentPrice || 150)}/day</p>
            </div>
          </div>
        </div>
      )}

      {battery.investorId && (
        <div className="bg-emerald-50 rounded-xl border border-emerald-200 p-5 mb-6">
          <div className="flex items-center justify-between mb-4">
            <div className="flex items-center gap-3">
              <div className="w-10 h-10 rounded-lg bg-emerald-100 flex items-center justify-center">
                <Handshake className="w-5 h-5 text-emerald-600" />
              </div>
              <div>
                <h3 className="font-semibold text-emerald-800">Investor Co-Ownership Details</h3>
                <span className="text-xs text-emerald-600 bg-emerald-100 px-2 py-0.5 rounded-full font-medium">Active Investment</span>
              </div>
            </div>
          </div>

          <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
            <div className="bg-white rounded-lg p-3 border border-emerald-100/50">
              <p className="text-xs text-emerald-600 mb-1">Investor Name</p>
              <Link href={`/admin/investors/${battery.investorId}`} className="font-bold text-slate-800 hover:text-accent hover:underline flex items-center gap-1">
                {battery.investorName} <TrendingUp className="w-3.5 h-3.5 text-emerald-500" />
              </Link>
              <p className="text-xs text-slate-400">ID: {battery.investorId}</p>
            </div>
            <div className="bg-white rounded-lg p-3 border border-emerald-100/50">
              <p className="text-xs text-emerald-600 mb-1">Ownership Share (%)</p>
              <p className="font-bold text-emerald-700 text-lg">{battery.investorSharePercentage}%</p>
              <p className="text-xs text-slate-400">Share of Daily Rent Revenue</p>
            </div>
            <div className="bg-white rounded-lg p-3 border border-emerald-100/50">
              <p className="text-xs text-emerald-600 mb-1">Invested Amount</p>
              <p className="font-bold text-slate-800">৳{(battery.investedAmount || 0).toLocaleString()}</p>
              <p className="text-xs text-slate-400">Total Capital Contributed</p>
            </div>
            <div className="bg-white rounded-lg p-3 border border-emerald-100/50">
              <p className="text-xs text-emerald-600 mb-1">Investor Daily Payout</p>
              <p className="font-bold text-emerald-600">৳{(((battery.rentPrice || 150) * (battery.investorSharePercentage || 0)) / 100).toLocaleString()}/day</p>
              <p className="text-xs text-slate-400">Calculated from Daily Rent</p>
            </div>
          </div>
        </div>
      )}

      {showEditModal && (
        <EditBatteryModal
          battery={battery}
          onSave={handleSaveEdit}
          onClose={() => setShowEditModal(false)}
        />
      )}
    </div>
  );
}

function EditBatteryModal({
  battery,
  onSave,
  onClose
}: {
  battery: Battery;
  onSave: (battery: Battery) => void;
  onClose: () => void;
}) {
  const [formData, setFormData] = useState<Battery>({ ...battery });

  const handleChange = (field: keyof Battery, value: any) => {
    setFormData({ ...formData, [field]: value });
  };

  return (
    <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
      <div className="bg-white rounded-xl shadow-xl w-full max-w-lg max-h-[90vh] overflow-y-auto">
        <div className="p-5 border-b border-slate-100 flex items-center justify-between sticky top-0 bg-white">
          <h2 className="text-lg font-bold text-slate-800">Edit Battery</h2>
          <button onClick={onClose} className="p-2 hover:bg-slate-100 rounded-lg">
            <X className="w-5 h-5 text-slate-400" />
          </button>
        </div>
        <div className="p-5 space-y-4">
          <div className="grid grid-cols-2 gap-4">
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Brand</label>
              <input
                type="text"
                value={formData.brand}
                onChange={(e) => handleChange('brand', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Model</label>
              <input
                type="text"
                value={formData.model}
                onChange={(e) => handleChange('model', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Serial Number</label>
              <input
                type="text"
                value={formData.serialNumber}
                onChange={(e) => handleChange('serialNumber', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Type</label>
              <select
                value={formData.type}
                onChange={(e) => handleChange('type', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              >
                <option value="lithium-ion">Lithium-Ion</option>
                <option value="lifepo4">LiFePO4</option>
                <option value="lead-acid">Lead Acid</option>
              </select>
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Capacity (Ah)</label>
              <input
                type="number"
                value={formData.capacity}
                onChange={(e) => handleChange('capacity', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Voltage (V)</label>
              <input
                type="number"
                value={formData.voltage}
                onChange={(e) => handleChange('voltage', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Purchase Date</label>
              <input
                type="date"
                value={formData.purchaseDate}
                onChange={(e) => handleChange('purchaseDate', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Purchase Price (৳)</label>
              <input
                type="number"
                value={formData.purchasePrice}
                onChange={(e) => handleChange('purchasePrice', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Warranty Expiry</label>
              <input
                type="date"
                value={formData.warrantyExpiry}
                onChange={(e) => handleChange('warrantyExpiry', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Deposit (৳) *</label>
              <input
                type="number"
                value={formData.deposit || 0}
                onChange={(e) => handleChange('deposit', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Daily Rent Price (৳) *</label>
              <input
                type="number"
                value={formData.rentPrice || 0}
                onChange={(e) => handleChange('rentPrice', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Status</label>
              <select
                value={formData.status}
                onChange={(e) => handleChange('status', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              >
                <option value="available">Available</option>
                <option value="in-use">In Use</option>
                <option value="charging">Charging</option>
                <option value="maintenance">Maintenance</option>
                <option value="retired">Retired</option>
              </select>
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Current SOC (%)</label>
              <input
                type="number"
                value={formData.currentSoc}
                onChange={(e) => handleChange('currentSoc', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
          </div>
          
          <div className="border-t border-slate-100 pt-4 mt-4">
            <h3 className="text-sm font-bold text-slate-800 mb-3">Investor Assignment (Co-Ownership)</h3>
            <div className="grid grid-cols-2 gap-4">
              <div>
                <label className="block text-sm font-medium text-slate-700 mb-1">Investor ID</label>
                <input
                  type="text"
                  value={formData.investorId || ''}
                  onChange={(e) => handleChange('investorId', e.target.value)}
                  placeholder="INV-001"
                  className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
                />
              </div>
              <div>
                <label className="block text-sm font-medium text-slate-700 mb-1">Investor Name</label>
                <input
                  type="text"
                  value={formData.investorName || ''}
                  onChange={(e) => handleChange('investorName', e.target.value)}
                  placeholder="Tahmid Rahman"
                  className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
                />
              </div>
              <div>
                <label className="block text-sm font-medium text-slate-700 mb-1">Ownership Share (%)</label>
                <input
                  type="number"
                  value={formData.investorSharePercentage || 0}
                  onChange={(e) => handleChange('investorSharePercentage', parseInt(e.target.value))}
                  placeholder="60"
                  className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
                />
              </div>
              <div>
                <label className="block text-sm font-medium text-slate-700 mb-1">Invested Amount (৳)</label>
                <input
                  type="number"
                  value={formData.investedAmount || 0}
                  onChange={(e) => handleChange('investedAmount', parseInt(e.target.value))}
                  placeholder="45000"
                  className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
                />
              </div>
            </div>
          </div>
        </div>
        <div className="p-5 border-t border-slate-100 flex gap-3 sticky bottom-0 bg-white">
          <button onClick={onClose} className="flex-1 py-2.5 px-4 border border-slate-200 rounded-lg font-semibold text-sm hover:bg-slate-50">
            Cancel
          </button>
          <button
            onClick={() => onSave(formData)}
            className="flex-1 py-2.5 px-4 bg-accent text-white rounded-lg font-semibold text-sm hover:bg-accent-dark"
          >
            Save Changes
          </button>
        </div>
      </div>
    </div>
  );
}

function BatteryRentalTab({ battery }: { battery: any }) {
  // Generate highly realistic rent transaction history
  const [transactions] = useState<any[]>(() => {
    const list: any[] = [];
    const riders = [battery.assignedBikerName || 'Sofiq Rahman', 'Karim Ahmed', 'Sajib Islam', 'Nayeem Chowdhury', 'Rakib Hasan'];
    const methods = ['bKash', 'Nagad', 'Rocket', 'Bank Transfer'];
    
    const days = 30;
    const baseDate = new Date();
    
    for (let i = 0; i < days; i++) {
      const date = new Date();
      date.setDate(baseDate.getDate() - i);
      const dateString = date.toISOString().split('T')[0];
      
      const riderIndex = (i) % riders.length;
      const methodIndex = (i + 1) % methods.length;
      
      // status distribution
      let status: 'paid' | 'pending' | 'failed' = 'paid';
      if (i === 1) status = 'pending';
      else if (i === 5) status = 'failed';
      
      const amount = battery.rentPrice || 150;

      list.push({
        id: `TX-BAT-${10400 + i}`,
        date: dateString,
        riderName: riders[riderIndex],
        duration: '1 Day',
        amount: amount,
        status: status,
        payoutMethod: methods[methodIndex]
      });
    }
    return list;
  });

  // Filter & Sorting State
  const [searchQuery, setSearchQuery] = useState('');
  const [statusFilter, setStatusFilter] = useState<string>('all');
  const [dateFrom, setDateFrom] = useState('');
  const [dateTo, setDateTo] = useState('');
  const [sortBy, setSortBy] = useState<'date' | 'amount' | 'rider'>('date');
  const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('desc');
  const [page, setPage] = useState(1);
  const pageSize = 8;

  // Handler functions
  const handleSort = (field: 'date' | 'amount' | 'rider') => {
    if (sortBy === field) {
      setSortOrder(prev => (prev === 'asc' ? 'desc' : 'asc'));
    } else {
      setSortBy(field);
      setSortOrder('desc');
    }
    setPage(1);
  };

  // Filter Logic
  const filteredTransactions = transactions.filter(tx => {
    if (statusFilter !== 'all' && tx.status !== statusFilter) return false;
    if (searchQuery && !tx.riderName.toLowerCase().includes(searchQuery.toLowerCase()) && !tx.id.toLowerCase().includes(searchQuery.toLowerCase())) return false;
    if (dateFrom && new Date(tx.date) < new Date(dateFrom)) return false;
    if (dateTo && new Date(tx.date) > new Date(dateTo)) return false;
    return true;
  });

  // Sort Logic
  const sortedTransactions = [...filteredTransactions].sort((a, b) => {
    let comparison = 0;
    if (sortBy === 'date') {
      comparison = new Date(a.date).getTime() - new Date(b.date).getTime();
    } else if (sortBy === 'amount') {
      comparison = a.amount - b.amount;
    } else if (sortBy === 'rider') {
      comparison = a.riderName.localeCompare(b.riderName);
    }
    return sortOrder === 'desc' ? -comparison : comparison;
  });

  // Pagination
  const totalPages = Math.ceil(sortedTransactions.length / pageSize);
  const paginatedTransactions = sortedTransactions.slice((page - 1) * pageSize, page * pageSize);

  const statusConfig: Record<string, { label: string; bg: string; color: string; icon: any }> = {
    paid: { label: 'Paid', bg: 'bg-green-100', color: 'text-green-700', icon: CheckCircle2 },
    pending: { label: 'Pending', bg: 'bg-amber-100', color: 'text-amber-700', icon: Clock },
    failed: { label: 'Failed', bg: 'bg-red-100', color: 'text-red-700', icon: XCircle },
  };

  const totalCollected = filteredTransactions
    .filter(t => t.status === 'paid')
    .reduce((sum, t) => sum + t.amount, 0);

  const pendingAmount = filteredTransactions
    .filter(t => t.status === 'pending')
    .reduce((sum, t) => sum + t.amount, 0);

  return (
    <div className="space-y-4 mt-6">
      <div className="flex items-center gap-2">
        <DollarSign className="w-5 h-5 text-green-600" />
        <h4 className="font-semibold text-slate-700 text-sm">Rental Transaction History</h4>
      </div>

      <div className="bg-white rounded-xl border border-slate-200 overflow-hidden">
        {/* Filters */}
        <div className="p-4 border-b border-slate-100 bg-slate-50/20">
          <div className="flex flex-col lg:flex-row lg:items-center lg:justify-between gap-3">
            
            <div className="flex flex-wrap items-center gap-2">
              <div className="relative">
                <Search className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-slate-400" />
                <input
                  type="text"
                  placeholder="Search by rider or ref..."
                  value={searchQuery}
                  onChange={(e) => { setSearchQuery(e.target.value); setPage(1); }}
                  className="pl-9 pr-4 py-2 border border-slate-200 rounded-lg text-sm w-48 sm:w-64 bg-white focus:outline-none focus:border-slate-400 transition-colors"
                />
              </div>
              
              <select
                value={statusFilter}
                onChange={(e) => { setStatusFilter(e.target.value); setPage(1); }}
                className="px-3 py-2 border border-slate-200 rounded-lg text-sm bg-white cursor-pointer"
              >
                <option value="all">All Status</option>
                <option value="paid">Paid</option>
                <option value="pending">Pending</option>
                <option value="failed">Failed</option>
              </select>
            </div>

            <div className="flex flex-wrap items-center gap-2">
              <div className="flex items-center gap-1">
                <input
                  type="date"
                  value={dateFrom}
                  onChange={(e) => { setDateFrom(e.target.value); setPage(1); }}
                  className="px-3 py-2 border border-slate-200 rounded-lg text-sm bg-white"
                />
                <span className="text-slate-400">to</span>
                <input
                  type="date"
                  value={dateTo}
                  onChange={(e) => { setDateTo(e.target.value); setPage(1); }}
                  className="px-3 py-2 border border-slate-200 rounded-lg text-sm bg-white"
                />
              </div>
              {(dateFrom || dateTo) && (
                <button
                  onClick={() => { setDateFrom(''); setDateTo(''); setPage(1); }}
                  className="px-2.5 py-1.5 text-xs text-red-500 hover:bg-red-50 rounded font-semibold transition-colors"
                >
                  Clear
                </button>
              )}
            </div>

          </div>
        </div>

        {/* Desktop Table View */}
        <div className="hidden lg:block overflow-x-auto">
          <table className="w-full text-left">
            <thead className="bg-slate-50 border-b border-slate-100">
              <tr>
                <th 
                  onClick={() => handleSort('date')}
                  className="px-4 py-3 text-xs font-semibold text-slate-500 uppercase tracking-wider cursor-pointer hover:bg-slate-100 transition-colors"
                >
                  <div className="flex items-center gap-1">
                    Date {sortBy === 'date' && <span className="text-accent">{sortOrder === 'asc' ? '↑' : '↓'}</span>}
                  </div>
                </th>
                <th className="px-4 py-3 text-xs font-semibold text-slate-500 uppercase tracking-wider">
                  Transaction ID
                </th>
                <th 
                  onClick={() => handleSort('rider')}
                  className="px-4 py-3 text-xs font-semibold text-slate-500 uppercase tracking-wider cursor-pointer hover:bg-slate-100 transition-colors"
                >
                  <div className="flex items-center gap-1">
                    Rider {sortBy === 'rider' && <span className="text-accent">{sortOrder === 'asc' ? '↑' : '↓'}</span>}
                  </div>
                </th>
                <th className="px-4 py-3 text-xs font-semibold text-slate-500 uppercase tracking-wider">
                  Duration
                </th>
                <th 
                  onClick={() => handleSort('amount')}
                  className="px-4 py-3 text-xs font-semibold text-slate-500 uppercase tracking-wider cursor-pointer hover:bg-slate-100 transition-colors text-right"
                >
                  <div className="flex items-center gap-1 justify-end">
                    Amount {sortBy === 'amount' && <span className="text-accent">{sortOrder === 'asc' ? '↑' : '↓'}</span>}
                  </div>
                </th>
                <th className="px-4 py-3 text-xs font-semibold text-slate-500 uppercase tracking-wider">
                  Method
                </th>
                <th className="px-4 py-3 text-xs font-semibold text-slate-500 uppercase tracking-wider">
                  Status
                </th>
              </tr>
            </thead>
            
            <tbody className="divide-y divide-slate-100">
              {paginatedTransactions.length > 0 ? (
                paginatedTransactions.map((tx) => {
                  const status = statusConfig[tx.status] || statusConfig.pending;
                  const StatusIcon = status.icon;
                  return (
                    <tr key={tx.id} className="hover:bg-slate-50 transition-colors">
                      <td className="px-4 py-3 text-sm text-slate-800 font-medium">
                        {tx.date}
                      </td>
                      <td className="px-4 py-3 text-xs font-mono font-semibold text-slate-400">
                        {tx.id}
                      </td>
                      <td className="px-4 py-3 text-sm">
                        <div className="flex items-center gap-2">
                          <User className="w-4 h-4 text-slate-400" />
                          <span className="font-semibold text-slate-700">{tx.riderName}</span>
                        </div>
                      </td>
                      <td className="px-4 py-3 text-sm text-slate-500 font-medium">
                        {tx.duration}
                      </td>
                      <td className="px-4 py-3 text-sm font-bold text-slate-800 text-right">
                        ৳{tx.amount.toLocaleString()}
                      </td>
                      <td className="px-4 py-3 text-sm text-slate-600 font-medium capitalize">
                        {tx.payoutMethod}
                      </td>
                      <td className="px-4 py-3">
                        <span className={`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-semibold ${status.bg} ${status.color}`}>
                          <StatusIcon className="w-3.5 h-3.5" />
                          {status.label}
                        </span>
                      </td>
                    </tr>
                  );
                })
              ) : (
                <tr>
                  <td colSpan={7} className="px-4 py-12 text-center text-slate-400">
                    <AlertCircle className="w-12 h-12 mx-auto mb-2 text-slate-300" />
                    <p className="text-sm font-semibold">No rental transactions found</p>
                  </td>
                </tr>
              )}
            </tbody>
          </table>
        </div>

        {/* Mobile View */}
        <div className="lg:hidden divide-y divide-slate-100">
          {paginatedTransactions.length > 0 ? (
            paginatedTransactions.map((tx) => {
              const status = statusConfig[tx.status] || statusConfig.pending;
              const StatusIcon = status.icon;
              return (
                <div key={tx.id} className="p-4 hover:bg-slate-50 transition-colors">
                  <div className="flex items-start justify-between gap-3 mb-2">
                    <div className="flex-1 min-w-0">
                      <div className="flex items-center gap-2">
                        <User className="w-4 h-4 text-slate-400 shrink-0" />
                        <p className="text-sm font-semibold text-slate-800 truncate">{tx.riderName}</p>
                      </div>
                      <p className="text-xs text-slate-400 ml-6">Ref: {tx.id}</p>
                    </div>
                    
                    <div className="text-right shrink-0">
                      <p className="text-base font-bold text-slate-800">৳{tx.amount.toLocaleString()}</p>
                      <span className={`inline-flex items-center gap-1 px-2 py-0.5 rounded text-xs font-semibold mt-1 ${status.bg} ${status.color}`}>
                        <StatusIcon className="w-3 h-3" /> {status.label}
                      </span>
                    </div>
                  </div>
                  
                  <div className="flex items-center justify-between ml-6 text-xs text-slate-400">
                    <span>{tx.date}</span>
                    <span className="capitalize">{tx.payoutMethod}</span>
                  </div>
                </div>
              );
            })
          ) : (
            <div className="p-8 text-center text-slate-500">
              <AlertCircle className="w-10 h-10 mx-auto mb-2 text-slate-300" />
              <p>No rental payments found</p>
            </div>
          )}
        </div>

        {/* Paginated Footer */}
        {sortedTransactions.length > pageSize && (
          <div className="p-4 border-t border-slate-100 flex flex-col sm:flex-row items-center justify-between gap-3 bg-slate-50/10">
            <p className="text-xs sm:text-sm text-slate-500">
              Showing {((page - 1) * pageSize) + 1} to {Math.min(page * pageSize, sortedTransactions.length)} of {sortedTransactions.length} records
            </p>
            
            <div className="flex items-center gap-1">
              <button
                onClick={() => setPage(p => Math.max(1, p - 1))}
                disabled={page === 1}
                className="p-2 border border-slate-200 rounded-lg disabled:opacity-50 hover:bg-slate-50 bg-white"
              >
                <ChevronLeft className="w-4 h-4 text-slate-600" />
              </button>
              
              {Array.from({ length: totalPages }, (_, i) => i + 1).map(pageNum => (
                <button
                  key={pageNum}
                  onClick={() => setPage(pageNum)}
                  className={`w-8 h-8 rounded-lg text-sm font-semibold transition-colors ${
                    page === pageNum 
                      ? 'bg-accent text-white' 
                      : 'border border-slate-200 hover:bg-slate-50 bg-white text-slate-600'
                  }`}
                >
                  {pageNum}
                </button>
              ))}

              <button
                onClick={() => setPage(p => Math.min(totalPages, p + 1))}
                disabled={page === totalPages}
                className="p-2 border border-slate-200 rounded-lg disabled:opacity-50 hover:bg-slate-50 bg-white"
              >
                <ChevronRight className="w-4 h-4 text-slate-600" />
              </button>
            </div>
          </div>
        )}

      </div>
    </div>
  );
}