import React, { useState, useEffect } from 'react';
import { X, Shield, Server, Database, Folder, Key, User, Download, AlertCircle } from 'lucide-react';

export default function BackupModal({ isOpen, onClose, store, currentDatabase, onConfirm }) {
    const [formData, setFormData] = useState({
        database: currentDatabase || '',
        host: import.meta.env.VITE_FTP_HOST || '',
        user: import.meta.env.VITE_FTP_USER || '',
        pass: import.meta.env.VITE_FTP_PASS || '',
        path: import.meta.env.VITE_FTP_PATH || '/'
    });

    useEffect(() => {
        if (currentDatabase) {
            setFormData(prev => ({ ...prev, database: currentDatabase }));
        }
    }, [currentDatabase]);

    if (!isOpen) return null;

    const handleSubmit = (e) => {
        e.preventDefault();
        onConfirm(formData);
    };

    return (
        <div className="bk-modal-overlay">
            <div className="bk-modal-content">
                {/* Header */}
                <div className="bk-modal-header">
                    <div className="flex items-center gap-2">
                        <Download size={20} style={{ color: 'var(--text-accent)' }} />
                        <h2 style={{ margin: 0, fontSize: '1rem', color: '#fff', fontWeight: 700, textTransform: 'uppercase', letterSpacing: '0.05em' }}>
                            Solicitar Respaldo DB
                        </h2>
                    </div>
                    <button onClick={onClose} className="bk-close-btn">
                        <X size={20} />
                    </button>
                </div>

                {/* Content */}
                <form onSubmit={handleSubmit} className="bk-modal-body">
                    <div className="bk-info-banner">
                        <AlertCircle size={18} style={{ color: 'var(--warning)', flexShrink: 0 }} />
                        <div style={{ fontSize: '0.75rem', color: 'var(--text-secondary)', lineHeight: '1.5' }}>
                            Se enviará un comando <code style={{ color: 'var(--text-accent)', background: 'rgba(0,0,0,0.3)', padding: '2px 4px', borderRadius: '4px' }}>BACKUP</code> al agente remoto de <b style={{ color: '#fff' }}>{store?.name}</b>.
                            El proceso puede tardar varios minutos.
                        </div>
                    </div>

                    <div className="bk-form-grid">
                        {/* Database Name */}
                        <div className="bk-input-group full-width">
                            <label><Database size={12} /> Base de Datos</label>
                            <input
                                type="text"
                                required
                                value={formData.database}
                                onChange={(e) => setFormData(prev => ({ ...prev, database: e.target.value }))}
                                placeholder="nombre_bd"
                            />
                        </div>

                        {/* FTP Host */}
                        <div className="bk-input-group">
                            <label><Server size={12} /> Host FTP</label>
                            <input
                                type="text"
                                required
                                value={formData.host}
                                onChange={(e) => setFormData(prev => ({ ...prev, host: e.target.value }))}
                                placeholder="ftp.servidor.com"
                            />
                        </div>

                        {/* FTP User */}
                        <div className="bk-input-group">
                            <label><User size={12} /> Usuario FTP</label>
                            <input
                                type="text"
                                required
                                value={formData.user}
                                onChange={(e) => setFormData(prev => ({ ...prev, user: e.target.value }))}
                                placeholder="usuario"
                            />
                        </div>

                        {/* FTP Password */}
                        <div className="bk-input-group">
                            <label><Key size={12} /> Contraseña FTP</label>
                            <input
                                type="password"
                                required
                                value={formData.pass}
                                onChange={(e) => setFormData(prev => ({ ...prev, pass: e.target.value }))}
                                placeholder="••••••••"
                            />
                        </div>

                        {/* FTP Path */}
                        <div className="bk-input-group">
                            <label><Folder size={12} /> Ruta Destino</label>
                            <input
                                type="text"
                                required
                                value={formData.path}
                                onChange={(e) => setFormData(prev => ({ ...prev, path: e.target.value }))}
                                placeholder="/respaldos/"
                            />
                        </div>
                    </div>

                    <div className="bk-modal-footer">
                        <button type="button" onClick={onClose} className="bk-btn-secondary">
                            Cancelar
                        </button>
                        <button type="submit" className="bk-btn-primary">
                            <Shield size={18} /> Iniciar Respaldo
                        </button>
                    </div>
                </form>
            </div>

            <style>{`
                .bk-modal-overlay {
                    position: fixed;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    background-color: rgba(0, 0, 0, 0.7);
                    backdrop-filter: blur(4px);
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    z-index: 1000;
                    padding: 1rem;
                }

                .bk-modal-content {
                    background-color: var(--bg-panel);
                    width: 100%;
                    max-width: 500px;
                    border-radius: 0.75rem;
                    border: 1px solid var(--border);
                    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
                    overflow: hidden;
                    animation: modalEntry 0.3s ease-out;
                }

                @keyframes modalEntry {
                    from { opacity: 0; transform: translateY(20px); }
                    to { opacity: 1; transform: translateY(0); }
                }

                .bk-modal-header {
                    background-color: var(--bg-input);
                    padding: 1rem 1.25rem;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    border-bottom: 1px solid var(--border);
                }

                .bk-close-btn {
                    background: transparent;
                    border: none;
                    color: var(--text-muted);
                    cursor: pointer;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    padding: 4px;
                    border-radius: 4px;
                }

                .bk-close-btn:hover {
                    color: #fff;
                    background-color: rgba(255,255,255,0.1);
                }

                .bk-modal-body {
                    padding: 1.5rem;
                    display: flex;
                    flex-direction: column;
                    gap: 1.25rem;
                }

                .bk-info-banner {
                    background-color: rgba(245, 158, 11, 0.1);
                    border: 1px solid rgba(245, 158, 11, 0.2);
                    padding: 0.75rem;
                    border-radius: 0.5rem;
                    display: flex;
                    gap: 0.75rem;
                    align-items: center;
                }

                .bk-form-grid {
                    display: grid;
                    grid-template-columns: 1fr 1fr;
                    gap: 1rem;
                }

                .bk-input-group {
                    display: flex;
                    flex-direction: column;
                    gap: 0.375rem;
                }

                .bk-input-group.full-width {
                    grid-column: span 2;
                }

                .bk-input-group label {
                    font-size: 0.7rem;
                    font-weight: 700;
                    color: var(--text-muted);
                    text-transform: uppercase;
                    letter-spacing: 0.05em;
                    display: flex;
                    align-items: center;
                    gap: 0.5rem;
                }

                .bk-input-group input {
                    background-color: var(--bg-input);
                    border: 1px solid var(--border);
                    color: #fff;
                    padding: 0.625rem 0.875rem;
                    border-radius: 0.5rem;
                    font-size: 0.875rem;
                    outline: none;
                    transition: border-color 0.2s;
                }

                .bk-input-group input:focus {
                    border-color: var(--accent);
                }

                .bk-modal-footer {
                    display: flex;
                    gap: 1rem;
                    margin-top: 0.5rem;
                }

                .bk-btn-secondary {
                    flex: 1;
                    padding: 0.75rem;
                    background: transparent;
                    border: 1px solid var(--border);
                    color: #fff;
                    border-radius: 0.5rem;
                    font-weight: 600;
                    cursor: pointer;
                    transition: all 0.2s;
                }

                .bk-btn-secondary:hover {
                    background-color: var(--bg-hover);
                }

                .bk-btn-primary {
                    flex: 1;
                    padding: 0.75rem;
                    background-color: var(--accent);
                    border: none;
                    color: #fff;
                    border-radius: 0.5rem;
                    font-weight: 600;
                    cursor: pointer;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    gap: 0.5rem;
                    transition: all 0.2s;
                    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
                }

                .bk-btn-primary:hover {
                    background-color: var(--accent-hover);
                    transform: translateY(-1px);
                }
            `}</style>
        </div>
    );
}
