'use client';

import { useEffect } from 'react';
import { useDeviceStore } from '@/stores/device-store';
import { useDeviceEvents } from '@/hooks/use-device-events';
import { DeviceList } from '@/components/devices/device-list';
import { Button } from '@/components/ui/button';
import { RefreshCw } from 'lucide-react';
import { useTranslation } from '@/lib/i18n';

export default function DevicesPage() {
  const { t } = useTranslation();
  const { devices, loading, scanning, fetchDevices, scanDevices } = useDeviceStore();

  useDeviceEvents();

  useEffect(() => {
    fetchDevices();
  }, [fetchDevices]);

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold">{t('devices.title')}</h1>
          <p className="text-muted-foreground">{t('devices.subtitle')}</p>
        </div>
        <Button onClick={scanDevices} disabled={scanning} variant="outline" data-tour-id="scan-devices-btn">
          <RefreshCw className={`mr-2 h-4 w-4 ${scanning ? 'animate-spin' : ''}`} />
          {scanning ? t('devices.scanning') : t('devices.scan')}
        </Button>
      </div>
      <DeviceList devices={devices} loading={loading} />
    </div>
  );
}
