<?php
require_once('config.php');
require_once('inc/header.php');

// ডাটাবেজ সংযোগ
$conn = new mysqli('localhost', 'mahamudf_skytimesphp', 'mahamudf_skytimesphp', 'mahamudf_skytimesphp');

// টেবিল তৈরি
$conn->query("CREATE TABLE IF NOT EXISTS `mahamudfutureit_tts_settings` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `setting_key` varchar(100) NOT NULL,
    `setting_value` text NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `setting_key` (`setting_key`)
)");

$conn->query("INSERT IGNORE INTO `mahamudfutureit_tts_settings` (`setting_key`, `setting_value`) VALUES 
    ('tts_enabled', '1'),
    ('tts_voice', 'male')");

// সেটিংস আপডেট
if(isset($_POST['save_tts_settings'])) {
    $tts_enabled = isset($_POST['tts_enabled']) ? '1' : '0';
    $tts_voice = $_POST['tts_voice'];
    
    $conn->query("INSERT INTO mahamudfutureit_tts_settings (setting_key, setting_value) 
                   VALUES ('tts_enabled', '$tts_enabled') 
                   ON DUPLICATE KEY UPDATE setting_value = '$tts_enabled'");
    
    $conn->query("INSERT INTO mahamudfutureit_tts_settings (setting_key, setting_value) 
                   VALUES ('tts_voice', '$tts_voice') 
                   ON DUPLICATE KEY UPDATE setting_value = '$tts_voice'");
    
    echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="bi bi-check-circle-fill"></i> TTS সেটিংস সংরক্ষিত হয়েছে!
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
          </div>';
}

// বর্তমান মান
$result = $conn->query("SELECT setting_value FROM mahamudfutureit_tts_settings WHERE setting_key='tts_enabled'");
$tts_enabled = $result->num_rows ? $result->fetch_assoc()['setting_value'] : '1';

$result = $conn->query("SELECT setting_value FROM mahamudfutureit_tts_settings WHERE setting_key='tts_voice'");
$tts_voice = $result->num_rows ? $result->fetch_assoc()['setting_value'] : 'male';
?>

<style>
.settings-card {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    margin-bottom: 25px;
    overflow: hidden;
}
.settings-card-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 15px 20px;
}
.settings-card-header h5 {
    margin: 0;
    font-size: 18px;
}
.settings-card-body {
    padding: 25px;
}
.form-switch-large .form-check-input {
    width: 50px;
    height: 25px;
}
.voice-option {
    display: inline-block;
    margin-right: 30px;
}
.voice-option input {
    margin-right: 8px;
}
</style>

<main id="main" class="main">
    <div class="row">
        <div class="col-12">
            <div class="pagetitle">
                <h1><i class="bi bi-headphones me-2"></i> অডিও নিউজ সেটিংস</h1>
                <nav>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.php">হোম</a></li>
                        <li class="breadcrumb-item active">অডিও নিউজ</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>

    <section class="section">
        <div class="row">
            <div class="col-lg-12">
                <div class="settings-card">
                    <div class="settings-card-header">
                        <h5><i class="bi bi-mic"></i> টেক্সট টু স্পিচ (TTS) সেটিংস</h5>
                    </div>
                    <div class="settings-card-body">
                        <form method="POST">
                            <div class="mb-4">
                                <label class="form-label fw-bold">🔊 TTS সক্রিয় করুন</label>
                                <div class="form-check form-switch form-switch-large">
                                    <input class="form-check-input" type="checkbox" name="tts_enabled" value="1" style="width: 50px; height: 25px; cursor: pointer;" <?php echo $tts_enabled == '1' ? 'checked' : ''; ?>>
                                    <label class="form-check-label ms-2 fw-bold"><?php echo $tts_enabled == '1' ? '✅ সক্রিয়' : '❌ নিষ্ক্রিয়'; ?></label>
                                </div>
                                <small class="text-muted">অন করলে নিউজ পেজে অডিও প্লেয়ার দেখাবে</small>
                            </div>
                            
                            <div class="mb-4">
                                <label class="form-label fw-bold">🎙️ ভয়েস সিলেক্ট করুন</label>
                                <div class="voice-option">
                                    <input type="radio" name="tts_voice" value="male" id="voiceMale" <?php echo $tts_voice == 'male' ? 'checked' : ''; ?>>
                                    <label for="voiceMale">👨 পুরুষ ভয়েস</label>
                                </div>
                                <div class="voice-option">
                                    <input type="radio" name="tts_voice" value="female" id="voiceFemale" <?php echo $tts_voice == 'female' ? 'checked' : ''; ?>>
                                    <label for="voiceFemale">👩 নারী ভয়েস</label>
                                </div>
                                <div><small class="text-muted">পুরুষ = হিন্দি, নারী = বাংলা (গুগল TTS)</small></div>
                            </div>
                            
                            <button type="submit" name="save_tts_settings" class="btn btn-primary">
                                <i class="bi bi-save"></i> সেটিংস সংরক্ষণ
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>

<?php require_once('inc/footer.php'); ?>