'use client';

import AppBar from '@mui/material/AppBar';
import Box from '@mui/material/Box';
import IconButton from '@mui/material/IconButton';
import Stack from '@mui/material/Stack';
import Toolbar from '@mui/material/Toolbar';
import useMediaQuery from '@mui/material/useMediaQuery';
import { styled, Theme } from '@mui/material/styles';
import MenuIcon from '@mui/icons-material/Menu';
import DarkModeIcon from '@mui/icons-material/DarkMode';
import LightModeIcon from '@mui/icons-material/LightMode';
import { useCustomizerStore } from '../store/useCustomizerStore';

import Profile from './Profile';
import Search from './Search';

const AppBarStyled = styled(AppBar)(({ theme }) => ({
  boxShadow: 'none',
  background: theme.palette.background.paper,
  justifyContent: 'center',
  backdropFilter: 'blur(4px)',
  zIndex: theme.zIndex.drawer + 1,
}));

const ToolbarStyled = styled(Toolbar)(({ theme }) => ({
  width: '100%',
  color: theme.palette.text.secondary,
}));

const Header = () => {
  const lgUp = useMediaQuery((theme: Theme) => theme.breakpoints.up('lg'));

  const {
    activeMode,
    toggleSidebar,
    toggleMobileSidebar,
    setDarkMode,
    isHydrated,
  } = useCustomizerStore();

  if (!isHydrated) {
    return null;
  }

  return (
    <AppBarStyled position="sticky" color="default">
      <ToolbarStyled>
        {/* ------------------------------------------- */}
        {/* Toggle Button Sidebar */}
        {/* ------------------------------------------- */}
        <IconButton
          color="inherit"
          aria-label="menu"
          onClick={lgUp ? toggleSidebar : toggleMobileSidebar}
        >
          <MenuIcon />
        </IconButton>

        {/* ------------------------------------------- */}
        {/* Search */}
        {/* ------------------------------------------- */}
        <Search />

        <Box flexGrow={1} />

        <Stack spacing={1} direction="row" alignItems="center">
          {/* ------------------------------------------- */}
          {/* Theme Toggle (Dark/Light) */}
          {/* ------------------------------------------- */}
          <IconButton
            size="large"
            color="inherit"
            onClick={() =>
              setDarkMode(activeMode === 'light' ? 'dark' : 'light')
            }
            aria-label="alternar tema"
          >
            {activeMode === 'light' ? <DarkModeIcon /> : <LightModeIcon />}
          </IconButton>

          {/* ------------------------------------------- */}
          {/* Profile */}
          {/* ------------------------------------------- */}
          <Profile />
        </Stack>
      </ToolbarStyled>
    </AppBarStyled>
  );
};

export default Header;

