<?php
// core.php - Database core + API endpoints
header('Content-Type: application/json; charset=utf-8');

// ================== EDIT THESE CREDENTIALS ==================
$host     = 'localhost';
$db_user  = 'tasmoybb_grammie';           // ← CHANGE TO YOUR MYSQL USER
$db_pass  = '3XT3RN4l!337';               // ← CHANGE TO YOUR MYSQL PASSWORD
$db_name  = 'tasmoybb_grammie'; // ← Will be created automatically
// ============================================================

$conn = new mysqli($host, $db_user, $db_pass);

if ($conn->connect_error) {
    die(json_encode(['success' => false, 'error' => 'Database connection failed']));
}

// Create database if it doesn't exist
$conn->query("CREATE DATABASE IF NOT EXISTS `$db_name`");
$conn->select_db($db_name);

// Create accounts table (stores all follower/following data)
$conn->query("
    CREATE TABLE IF NOT EXISTS accounts (
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(255) UNIQUE NOT NULL,
        href VARCHAR(500),
        timestamp BIGINT DEFAULT 0,
        is_following TINYINT(1) DEFAULT 0,
        is_followed_by TINYINT(1) DEFAULT 0,
        imported_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
");

$action = $_GET['action'] ?? '';

if ($action === 'get_nonfollowers') {
    $result = $conn->query("
        SELECT username, href 
        FROM accounts 
        WHERE is_following = 1 AND is_followed_by = 0 
        ORDER BY username ASC
    ");
    $accounts = [];
    while ($row = $result->fetch_assoc()) $accounts[] = $row;

    // Stats
    $stats = [
        'non' => $accounts ? count($accounts) : 0,
        'mutual' => (int) $conn->query("SELECT COUNT(*) FROM accounts WHERE is_following=1 AND is_followed_by=1")->fetch_row()[0],
        'following' => (int) $conn->query("SELECT COUNT(*) FROM accounts WHERE is_following=1")->fetch_row()[0],
        'followers' => (int) $conn->query("SELECT COUNT(*) FROM accounts WHERE is_followed_by=1")->fetch_row()[0]
    ];

    echo json_encode(['success' => true, 'accounts' => $accounts, 'stats' => $stats]);
    
} elseif ($action === 'get_mutuals') {
    $result = $conn->query("
        SELECT username, href 
        FROM accounts 
        WHERE is_following = 1 AND is_followed_by = 1 
        ORDER BY username ASC
    ");
    $accounts = [];
    while ($row = $result->fetch_assoc()) $accounts[] = $row;
    echo json_encode(['success' => true, 'accounts' => $accounts]);
    
} elseif ($action === 'get_following') {
    $result = $conn->query("
        SELECT username, href 
        FROM accounts 
        WHERE is_following = 1 
        ORDER BY username ASC
    ");
    $accounts = [];
    while ($row = $result->fetch_assoc()) $accounts[] = $row;
    echo json_encode(['success' => true, 'accounts' => $accounts]);
    
} elseif ($action === 'get_followers') {
    $result = $conn->query("
        SELECT username, href 
        FROM accounts 
        WHERE is_followed_by = 1 
        ORDER BY username ASC
    ");
    $accounts = [];
    while ($row = $result->fetch_assoc()) $accounts[] = $row;
    echo json_encode(['success' => true, 'accounts' => $accounts]);
    
} else {
    echo json_encode(['success' => false, 'error' => 'Invalid action']);
}

$conn->close();
?>