import { useState } from 'react';
import {
  Box,
  Container,
  Typography,
  Card,
  CardContent,
  Button,
  Checkbox,
  FormControlLabel,
  Select,
  MenuItem,
  TextField,
  FormControl,
  InputLabel,
  IconButton,
  Radio,
  RadioGroup,
} from '@mui/material';
import { ArrowBack } from '@mui/icons-material';

// 디자인 시스템 색상
const colors = {
  pink: '#F472B6',
  purple: '#C084FC',
  purpleLight: '#E9D5FF',
  blue: '#60A5FA',
  mint: '#34D399',
  orange: '#FB923C',
  yellow: '#FBBF24',
  gray: {
    900: '#1A1A1A',
    700: '#4A4A4A',
    500: '#9E9E9E',
    300: '#D9D9D9',
    100: '#F5F5F5',
  },
};

interface Channel {
  id: string;
  name: string;
  selected: boolean;
  config?: Record<string, string | number>;
}

interface ChannelStepProps {
  onNext: (channels: string[]) => void;
  onBack: () => void;
}

export default function ChannelStep({ onNext, onBack }: ChannelStepProps) {
  const [channels, setChannels] = useState<Channel[]>([
    { id: 'uriTV', name: '우리동네TV', selected: false, config: { radius: '500', time: 'evening' } },
    { id: 'ringoBiz', name: '링고비즈', selected: false, config: { phone: '010-1234-5678' } },
    { id: 'genieTV', name: '지니TV 광고', selected: false, config: { region: 'suwon', time: 'all', budget: '' } },
    { id: 'sns', name: 'SNS', selected: false, config: { instagram: 'true', naver: 'true', kakao: 'false', schedule: 'now' } },
  ]);

  const handleChannelToggle = (channelId: string) => {
    setChannels((prev) =>
      prev.map((ch) => (ch.id === channelId ? { ...ch, selected: !ch.selected } : ch))
    );
  };

  const handleConfigChange = (channelId: string, key: string, value: string) => {
    setChannels((prev) =>
      prev.map((ch) =>
        ch.id === channelId ? { ...ch, config: { ...ch.config, [key]: value } } : ch
      )
    );
  };

  const getChannelConfig = (channelId: string, key: string): string => {
    const channel = channels.find((ch) => ch.id === channelId);
    return (channel?.config?.[key] as string) || '';
  };

  const calculateSummary = () => {
    let totalCost = 0;
    let totalExposure = 0;

    channels.forEach((ch) => {
      if (!ch.selected) return;

      if (ch.id === 'uriTV') {
        totalCost += 80000;
        totalExposure += 50000;
      } else if (ch.id === 'ringoBiz') {
        totalExposure += 30000;
      } else if (ch.id === 'genieTV') {
        const budget = parseInt(getChannelConfig('genieTV', 'budget')) || 0;
        totalCost += budget;
        totalExposure += Math.floor(budget / 100) * 1000;
      }
    });

    return { totalCost, totalExposure };
  };

  const handleNext = () => {
    const selectedChannels = channels.filter((ch) => ch.selected).map((ch) => ch.id);
    if (selectedChannels.length > 0) {
      onNext(selectedChannels);
    }
  };

  const { totalCost, totalExposure } = calculateSummary();
  const selectedCount = channels.filter((ch) => ch.selected).length;

  return (
    <Box sx={{ minHeight: '100vh', bgcolor: 'background.default', pb: 20 }}>
      <Container maxWidth="lg" sx={{ pt: { xs: 2, sm: 8 }, pb: { xs: 3, sm: 8 }, px: { xs: 2, sm: 6, md: 8 } }}>
        {/* Header */}
        <Box sx={{ display: 'flex', alignItems: 'center', gap: { xs: 1, sm: 3 }, mb: { xs: 3, sm: 8 } }}>
          <IconButton onClick={onBack} sx={{ width: { xs: 36, sm: 40 }, height: { xs: 36, sm: 40 }, p: { xs: 0.5, sm: 1 } }}>
            <ArrowBack sx={{ fontSize: { xs: 18, sm: 20 } }} />
          </IconButton>
          <Typography variant="h5" sx={{ fontWeight: 700, fontSize: { xs: '1rem', sm: '1.5rem' } }}>
            배포 채널 선택
          </Typography>
        </Box>

        <Typography variant="body2" color="text.secondary" sx={{ mb: { xs: 2.5, sm: 8 }, textAlign: 'center', fontSize: { xs: '0.75rem', sm: '1rem' } }}>
          (최소 1개 이상)
        </Typography>

        {/* 우리동네TV */}
        <Card
          elevation={0}
          sx={{
            mb: { xs: 2.5, sm: 6 },
            borderRadius: { xs: 3, sm: 4 },
            border: channels[0].selected ? 2 : 1,
            borderColor: channels[0].selected ? colors.purple : 'divider',
            bgcolor: channels[0].selected ? `${colors.purpleLight}40` : 'background.paper',
            boxShadow: channels[0].selected ? '0 4px 12px rgba(0, 0, 0, 0.15)' : '0 2px 8px rgba(0, 0, 0, 0.08)',
            transition: 'all 0.3s',
          }}
        >
          <CardContent sx={{ p: { xs: 2.5, sm: 6 } }}>
            <FormControlLabel
              control={
                <Checkbox
                  checked={channels[0].selected}
                  onChange={() => handleChannelToggle('uriTV')}
                  sx={{
                    p: { xs: 0.5, sm: 1 },
                    color: colors.purple,
                    '&.Mui-checked': {
                      color: colors.purple,
                    },
                  }}
                />
              }
              label={
                <Typography variant="body1" sx={{ fontWeight: channels[0].selected ? 700 : 600, fontSize: { xs: '0.9375rem', sm: '1.125rem' } }}>
                  우리동네TV
                </Typography>
              }
              sx={{ mb: channels[0].selected ? { xs: 1.5, sm: 2 } : 0 }}
            />

            {channels[0].selected && (
              <Box sx={{ pl: { xs: 2.5, sm: 4 }, pt: { xs: 1.5, sm: 2 }, borderTop: 1, borderColor: 'divider' }}>
                <FormControl fullWidth sx={{ mb: { xs: 1.5, sm: 2 } }}>
                  <InputLabel sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>반경</InputLabel>
                  <Select
                    value={getChannelConfig('uriTV', 'radius')}
                    onChange={(e) => handleConfigChange('uriTV', 'radius', e.target.value)}
                    label="반경"
                    sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}
                  >
                    <MenuItem value="500" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>500m</MenuItem>
                    <MenuItem value="1000" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>1km</MenuItem>
                    <MenuItem value="2000" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>2km</MenuItem>
                  </Select>
                </FormControl>

                <FormControl fullWidth sx={{ mb: { xs: 1.5, sm: 2 } }}>
                  <InputLabel sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>노출 시간대</InputLabel>
                  <Select
                    value={getChannelConfig('uriTV', 'time')}
                    onChange={(e) => handleConfigChange('uriTV', 'time', e.target.value)}
                    label="노출 시간대"
                    sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}
                  >
                    <MenuItem value="morning" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>아침 (7-12시)</MenuItem>
                    <MenuItem value="afternoon" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>점심 (12-17시)</MenuItem>
                    <MenuItem value="evening" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>저녁 (17-22시)</MenuItem>
                    <MenuItem value="all" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>전체</MenuItem>
                  </Select>
                </FormControl>

                <Typography variant="body2" color="text.secondary" sx={{ fontSize: { xs: '0.75rem', sm: '0.875rem' }, mb: { xs: 0.5, sm: 0 } }}>
                  예상 노출: <strong>5만명</strong>
                </Typography>
                <Typography variant="body2" color="text.secondary" sx={{ fontSize: { xs: '0.75rem', sm: '0.875rem' } }}>
                  비용: <strong>8만원</strong>
                </Typography>
              </Box>
            )}
          </CardContent>
        </Card>

        {/* 링고비즈 */}
        <Card
          elevation={0}
          sx={{
            mb: { xs: 2.5, sm: 6 },
            borderRadius: { xs: 3, sm: 4 },
            border: channels[1].selected ? 2 : 1,
            borderColor: channels[1].selected ? colors.purple : 'divider',
            bgcolor: channels[1].selected ? `${colors.purpleLight}40` : 'background.paper',
            boxShadow: channels[1].selected ? '0 4px 12px rgba(0, 0, 0, 0.15)' : '0 2px 8px rgba(0, 0, 0, 0.08)',
            transition: 'all 0.3s',
          }}
        >
          <CardContent sx={{ p: { xs: 2.5, sm: 6 } }}>
            <FormControlLabel
              control={
                <Checkbox
                  checked={channels[1].selected}
                  onChange={() => handleChannelToggle('ringoBiz')}
                  sx={{
                    p: { xs: 0.5, sm: 1 },
                    color: colors.purple,
                    '&.Mui-checked': {
                      color: colors.purple,
                    },
                  }}
                />
              }
              label={
                <Typography variant="body1" sx={{ fontWeight: channels[1].selected ? 700 : 600, fontSize: { xs: '0.9375rem', sm: '1.125rem' } }}>
                  링고비즈
                </Typography>
              }
              sx={{ mb: channels[1].selected ? { xs: 1.5, sm: 2 } : 0 }}
            />

            {channels[1].selected && (
              <Box sx={{ pl: { xs: 2.5, sm: 4 }, pt: { xs: 1.5, sm: 2 }, borderTop: 1, borderColor: 'divider' }}>
                <TextField
                  fullWidth
                  label="매장 전화번호"
                  value={getChannelConfig('ringoBiz', 'phone')}
                  InputProps={{ readOnly: true }}
                  sx={{ mb: { xs: 1.5, sm: 2 } }}
                  InputLabelProps={{ sx: { fontSize: { xs: '0.875rem', sm: '1rem' } } }}
                  inputProps={{ sx: { fontSize: { xs: '0.875rem', sm: '1rem' } } }}
                />

                <Typography variant="body2" color="text.secondary" sx={{ mb: { xs: 0.5, sm: 0.5 }, fontSize: { xs: '0.75rem', sm: '0.875rem' } }}>
                  연결음 자동 업데이트
                </Typography>
                <Typography variant="body2" color="text.secondary" sx={{ fontSize: { xs: '0.75rem', sm: '0.875rem' }, mb: { xs: 0.5, sm: 0 } }}>
                  예상 노출: <strong>3만명</strong>
                </Typography>
                <Typography variant="body2" color="text.secondary" sx={{ fontSize: { xs: '0.75rem', sm: '0.875rem' } }}>
                  비용: <strong>무료</strong>
                </Typography>
              </Box>
            )}
          </CardContent>
        </Card>

        {/* 지니TV 광고 */}
        <Card
          elevation={0}
          sx={{
            mb: { xs: 2.5, sm: 6 },
            borderRadius: { xs: 3, sm: 4 },
            border: channels[2].selected ? 2 : 1,
            borderColor: channels[2].selected ? colors.purple : 'divider',
            bgcolor: channels[2].selected ? `${colors.purpleLight}40` : 'background.paper',
            boxShadow: channels[2].selected ? '0 4px 12px rgba(0, 0, 0, 0.15)' : '0 2px 8px rgba(0, 0, 0, 0.08)',
            transition: 'all 0.3s',
          }}
        >
          <CardContent sx={{ p: { xs: 2.5, sm: 6 } }}>
            <FormControlLabel
              control={
                <Checkbox
                  checked={channels[2].selected}
                  onChange={() => handleChannelToggle('genieTV')}
                  sx={{
                    p: { xs: 0.5, sm: 1 },
                    color: colors.purple,
                    '&.Mui-checked': {
                      color: colors.purple,
                    },
                  }}
                />
              }
              label={
                <Typography variant="body1" sx={{ fontWeight: channels[2].selected ? 700 : 600, fontSize: { xs: '0.9375rem', sm: '1.125rem' } }}>
                  지니TV 광고
                </Typography>
              }
              sx={{ mb: channels[2].selected ? { xs: 1.5, sm: 2 } : 0 }}
            />

            {channels[2].selected && (
              <Box sx={{ pl: { xs: 2.5, sm: 4 }, pt: { xs: 1.5, sm: 2 }, borderTop: 1, borderColor: 'divider' }}>
                <FormControl fullWidth sx={{ mb: { xs: 1.5, sm: 2 } }}>
                  <InputLabel sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>지역</InputLabel>
                  <Select
                    value={getChannelConfig('genieTV', 'region')}
                    onChange={(e) => handleConfigChange('genieTV', 'region', e.target.value)}
                    label="지역"
                    sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}
                  >
                    <MenuItem value="suwon" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>수원</MenuItem>
                    <MenuItem value="seoul" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>서울</MenuItem>
                    <MenuItem value="busan" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>부산</MenuItem>
                  </Select>
                </FormControl>

                <FormControl fullWidth sx={{ mb: { xs: 1.5, sm: 2 } }}>
                  <InputLabel sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>노출 시간대</InputLabel>
                  <Select
                    value={getChannelConfig('genieTV', 'time')}
                    onChange={(e) => handleConfigChange('genieTV', 'time', e.target.value)}
                    label="노출 시간대"
                    sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}
                  >
                    <MenuItem value="all" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>전체</MenuItem>
                    <MenuItem value="prime" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>프라임 (19-23시)</MenuItem>
                  </Select>
                </FormControl>

                <TextField
                  fullWidth
                  type="number"
                  label="예산"
                  placeholder="예산을 입력하세요"
                  value={getChannelConfig('genieTV', 'budget')}
                  onChange={(e) => handleConfigChange('genieTV', 'budget', e.target.value)}
                  InputProps={{ inputProps: { min: 0, step: 10000 } }}
                  sx={{ mb: { xs: 1.5, sm: 2 } }}
                  InputLabelProps={{ sx: { fontSize: { xs: '0.875rem', sm: '1rem' } } }}
                  inputProps={{ sx: { fontSize: { xs: '0.875rem', sm: '1rem' } } }}
                />

                <Typography variant="body2" color="text.secondary" sx={{ fontSize: { xs: '0.75rem', sm: '0.875rem' } }}>
                  예상 노출:{' '}
                  <strong>
                    {getChannelConfig('genieTV', 'budget')
                      ? `${(Math.floor(parseInt(getChannelConfig('genieTV', 'budget')) / 100) * 1000 / 10000).toFixed(1)}만명`
                      : '계산중...'}
                  </strong>
                </Typography>
              </Box>
            )}
          </CardContent>
        </Card>

        {/* SNS */}
        <Card
          elevation={0}
          sx={{
            mb: { xs: 3, sm: 10 },
            borderRadius: { xs: 3, sm: 4 },
            border: channels[3].selected ? 2 : 1,
            borderColor: channels[3].selected ? colors.purple : 'divider',
            bgcolor: channels[3].selected ? `${colors.purpleLight}40` : 'background.paper',
            boxShadow: channels[3].selected ? '0 4px 12px rgba(0, 0, 0, 0.15)' : '0 2px 8px rgba(0, 0, 0, 0.08)',
            transition: 'all 0.3s',
          }}
        >
          <CardContent sx={{ p: { xs: 2.5, sm: 6 } }}>
            <FormControlLabel
              control={
                <Checkbox
                  checked={channels[3].selected}
                  onChange={() => handleChannelToggle('sns')}
                  sx={{
                    p: { xs: 0.5, sm: 1 },
                    color: colors.purple,
                    '&.Mui-checked': {
                      color: colors.purple,
                    },
                  }}
                />
              }
              label={
                <Typography variant="body1" sx={{ fontWeight: channels[3].selected ? 700 : 600, fontSize: { xs: '0.9375rem', sm: '1.125rem' } }}>
                  SNS
                </Typography>
              }
              sx={{ mb: channels[3].selected ? { xs: 1.5, sm: 2 } : 0 }}
            />

            {channels[3].selected && (
              <Box sx={{ pl: { xs: 2.5, sm: 4 }, pt: { xs: 1.5, sm: 2 }, borderTop: 1, borderColor: 'divider' }}>
                <Typography variant="body2" sx={{ mb: { xs: 1, sm: 1 }, fontWeight: 600, fontSize: { xs: '0.8125rem', sm: '0.875rem' } }}>
                  플랫폼 선택
                </Typography>
                <FormControlLabel
                  control={
                    <Checkbox
                      checked={getChannelConfig('sns', 'instagram') === 'true'}
                      onChange={(e) =>
                        handleConfigChange('sns', 'instagram', e.target.checked.toString())
                      }
                      sx={{
                        p: { xs: 0.5, sm: 1 },
                        color: colors.purple,
                        '&.Mui-checked': {
                          color: colors.purple,
                        },
                      }}
                    />
                  }
                  label={<Typography sx={{ fontSize: { xs: '0.8125rem', sm: '0.875rem' } }}>Instagram</Typography>}
                  sx={{ display: 'block' }}
                />
                <FormControlLabel
                  control={
                    <Checkbox
                      checked={getChannelConfig('sns', 'naver') === 'true'}
                      onChange={(e) =>
                        handleConfigChange('sns', 'naver', e.target.checked.toString())
                      }
                      sx={{
                        p: { xs: 0.5, sm: 1 },
                        color: colors.purple,
                        '&.Mui-checked': {
                          color: colors.purple,
                        },
                      }}
                    />
                  }
                  label={<Typography sx={{ fontSize: { xs: '0.8125rem', sm: '0.875rem' } }}>Naver Blog</Typography>}
                  sx={{ display: 'block' }}
                />
                <FormControlLabel
                  control={
                    <Checkbox
                      checked={getChannelConfig('sns', 'kakao') === 'true'}
                      onChange={(e) =>
                        handleConfigChange('sns', 'kakao', e.target.checked.toString())
                      }
                      sx={{
                        p: { xs: 0.5, sm: 1 },
                        color: colors.purple,
                        '&.Mui-checked': {
                          color: colors.purple,
                        },
                      }}
                    />
                  }
                  label={<Typography sx={{ fontSize: { xs: '0.8125rem', sm: '0.875rem' } }}>Kakao Channel</Typography>}
                  sx={{ display: 'block', mb: { xs: 1.5, sm: 2 } }}
                />

                <FormControl fullWidth sx={{ mb: { xs: 1.5, sm: 2 } }}>
                  <InputLabel sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>예약 게시</InputLabel>
                  <Select
                    value={getChannelConfig('sns', 'schedule')}
                    onChange={(e) => handleConfigChange('sns', 'schedule', e.target.value)}
                    label="예약 게시"
                    sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}
                  >
                    <MenuItem value="now" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>즉시</MenuItem>
                    <MenuItem value="schedule" sx={{ fontSize: { xs: '0.875rem', sm: '1rem' } }}>예약</MenuItem>
                  </Select>
                </FormControl>

                <Typography variant="body2" color="text.secondary" sx={{ fontSize: { xs: '0.75rem', sm: '0.875rem' }, mb: { xs: 0.5, sm: 0 } }}>
                  예상 노출: <strong>-</strong>
                </Typography>
                <Typography variant="body2" color="text.secondary" sx={{ fontSize: { xs: '0.75rem', sm: '0.875rem' } }}>
                  비용: <strong>무료</strong>
                </Typography>
              </Box>
            )}
          </CardContent>
        </Card>

        {/* Summary */}
        <Card
          elevation={0}
          sx={{
            mb: { xs: 3, sm: 10 },
            borderRadius: { xs: 3, sm: 4 },
            bgcolor: 'grey.50',
            boxShadow: '0 2px 8px rgba(0, 0, 0, 0.08)',
          }}
        >
          <CardContent sx={{ p: { xs: 2.5, sm: 8 } }}>
            <Box sx={{ display: 'flex', justifyContent: 'space-between', mb: { xs: 2.5, sm: 4 } }}>
              <Typography variant="h6" sx={{ fontSize: { xs: '0.9375rem', sm: '1.25rem' } }}>
                총 예상 비용
              </Typography>
              <Typography variant="h6" color="error.main" sx={{ fontWeight: 700, fontSize: { xs: '0.9375rem', sm: '1.25rem' } }}>
                {totalCost.toLocaleString()}원
              </Typography>
            </Box>
            <Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
              <Typography variant="h6" sx={{ fontSize: { xs: '0.9375rem', sm: '1.25rem' } }}>
                총 예상 노출
              </Typography>
              <Typography variant="h6" sx={{ fontWeight: 700, fontSize: { xs: '0.9375rem', sm: '1.25rem' }, color: colors.purple }}>
                {totalExposure > 0 ? `${totalExposure.toLocaleString()}명+` : '0명'}
              </Typography>
            </Box>
          </CardContent>
        </Card>

        {/* Action Buttons */}
        <Box sx={{ display: 'flex', gap: { xs: 1.5, sm: 4 } }}>
          <Button
            fullWidth
            variant="outlined"
            size="large"
            onClick={onBack}
            sx={{
              py: { xs: 1.25, sm: 3 },
              borderRadius: { xs: 2.5, sm: 3 },
              fontSize: { xs: '0.8125rem', sm: '1rem' },
              fontWeight: 600,
              borderWidth: 2,
              '&:hover': {
                borderWidth: 2,
              },
            }}
          >
            이전
          </Button>
          <Button
            fullWidth
            variant="contained"
            size="large"
            disabled={selectedCount === 0}
            onClick={handleNext}
            sx={{
              py: { xs: 1.25, sm: 3 },
              borderRadius: { xs: 2.5, sm: 3 },
              fontSize: { xs: '0.8125rem', sm: '1rem' },
              fontWeight: 700,
              background: `linear-gradient(135deg, ${colors.purple} 0%, ${colors.pink} 100%)`,
              '&:hover': {
                background: `linear-gradient(135deg, ${colors.purple} 0%, ${colors.pink} 100%)`,
                opacity: 0.9,
              },
              '&:disabled': {
                background: colors.gray[300],
              },
            }}
          >
            다음
          </Button>
        </Box>
      </Container>
    </Box>
  );
}
