'use client';

import * as React from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Tab from '@mui/material/Tab';
import Tabs from '@mui/material/Tabs';
import Typography from '@mui/material/Typography';

import Users from '@/interfaces/users';
import VolunteerPage from '@/components/dashboard/volunteers/VolunteerPage';
import { VolunteersProvider } from '@/contexts/VolunteersContext';
import { paths } from '@/paths';

type ContactsTab = 'users' | 'volunteers';

function a11yProps(tab: ContactsTab) {
  return {
    id: `contacts-tab-${tab}`,
    'aria-controls': `contacts-tabpanel-${tab}`,
  };
}

export default function ContactsPage(): React.JSX.Element {
  const router = useRouter();
  const searchParams = useSearchParams();

  const tabParam = searchParams.get('tab');
  const activeTab: ContactsTab = tabParam === 'volunteers' ? 'volunteers' : 'users';

  const handleTabChange = (_: React.SyntheticEvent, value: ContactsTab) => {
    const next = new URLSearchParams(searchParams.toString());
    next.set('tab', value);

    // Keep user filters only on the users tab
    if (value === 'volunteers') {
      next.delete('status');
      next.delete('focus');
    } else if (value === 'users' && !next.get('tab')) {
      next.set('tab', 'users');
    }

    router.replace(`${paths.dashboard.contacts}?${next.toString()}`);
  };

  return (
    <Box sx={{ bgcolor: 'background.default', minHeight: '100vh', py: 2 }}>
      <Stack
        spacing={1}
        sx={{
          mb: 3,
          p: 3,
          borderRadius: 4,
          background: '#FFDD31',
          color: '#0B0504',
        }}
      >
        <Typography variant="h4" fontWeight={700}>
          Contacts
        </Typography>
        <Typography variant="body1" sx={{ opacity: 0.9 }}>
          Manage users and volunteer applications in one place
        </Typography>
      </Stack>

      <Box sx={{ borderBottom: 1, borderColor: 'divider', mb: 3 }}>
        <Tabs
          value={activeTab}
          onChange={handleTabChange}
          aria-label="Contacts tabs"
          textColor="primary"
          indicatorColor="primary"
        >
          <Tab label="Users" value="users" {...a11yProps('users')} />
          <Tab label="Volunteers" value="volunteers" {...a11yProps('volunteers')} />
        </Tabs>
      </Box>

      <Box
        role="tabpanel"
        hidden={activeTab !== 'users'}
        id="contacts-tabpanel-users"
        aria-labelledby="contacts-tab-users"
      >
        {activeTab === 'users' && <Users embedded />}
      </Box>

      <Box
        role="tabpanel"
        hidden={activeTab !== 'volunteers'}
        id="contacts-tabpanel-volunteers"
        aria-labelledby="contacts-tab-volunteers"
      >
        {activeTab === 'volunteers' && (
          <VolunteersProvider>
            <VolunteerPage embedded />
          </VolunteersProvider>
        )}
      </Box>
    </Box>
  );
}
