<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once __DIR__ . '/../includes/helpers.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

check_role('administrador');

$user = get_logged_in_user();

$user_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$user_id) {
    header('Location: companies.php');
    exit;
}

// Get user details
$stmt = $pdo->prepare("SELECT u.*, r.name as role_name FROM users u JOIN roles r ON u.role_id = r.id WHERE u.id = ?");
$stmt->execute([$user_id]);
$user_data = $stmt->fetch();

if (!$user_data || $user_data['role_name'] !== 'empresa') {
    header('Location: companies.php');
    exit;
}

// Get company-specific information
$stmt = $pdo->prepare("SELECT * FROM companies WHERE user_id = ?");
$stmt->execute([$user_id]);
$company_info = $stmt->fetch();

$page_title = 'Detalles de la Empresa - ' . htmlspecialchars($company_info['company_name'] ?? $user_data['first_name'] . ' ' . $user_data['last_name']);

$content = '
<div class="row">
    <div class="col-md-4">
        <div class="card card-primary card-outline">
            <div class="card-body box-profile">
                <div class="text-center">
                    <img class="profile-user-img img-fluid img-circle" src="' . ((isset($user_data['photo']) && !empty($user_data['photo']) && file_exists(BASE_PATH . '/' . $user_data['photo'])) ? url($user_data['photo']) : url('assets/img/avatar.png')) . '" alt="Contact photo" style="max-height: 100px; max-width: 100px; object-fit: cover;">
                </div>
                <h3 class="profile-username text-center">' . htmlspecialchars($company_info['company_name'] ?? $user_data['first_name'] . ' ' . $user_data['last_name']) . '</h3>
                <p class="text-muted text-center">Empresa</p>
                <ul class="list-group list-group-unbordered mb-3">
                    <li class="list-group-item">
                        <b>Username</b> <a class="float-right">' . htmlspecialchars($user_data['username']) . '</a>
                    </li>
                    <li class="list-group-item">
                        <b>Email</b> <a class="float-right">' . htmlspecialchars($user_data['email']) . '</a>
                    </li>
                    <li class="list-group-item">
                        <b>Registro</b> <a class="float-right">' . date('d/m/Y', strtotime($user_data['created_at'])) . '</a>
                    </li>
                </ul>
                <div class="text-center">
                    <a href="' . url('admin/companies') . '" class="btn btn-secondary btn-sm"><i class="fas fa-arrow-left"></i> Volver</a>
                </div>
            </div>
        </div>

        <div class="card mt-3">
            <div class="card-header">
                <h3 class="card-title">Logo de la Empresa</h3>
            </div>
            <div class="card-body text-center">
                ' . ((isset($company_info['logo']) && !empty($company_info['logo']) && file_exists(BASE_PATH . '/' . $company_info['logo'])) ? '<img src="' . url($company_info['logo']) . '" class="img-fluid" style="max-height: 120px;" alt="Company logo">' : '<div class="text-muted"><i class="fas fa-building fa-3x mb-2"></i><br>No hay logo</div>') . '
            </div>
        </div>
    </div>
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Información de la Empresa</h3>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-sm-6">
                        <strong><i class="fas fa-building mr-1"></i> Razón Social</strong>
                        <p class="text-muted">' . htmlspecialchars($company_info['company_name'] ?? 'No especificada') . '</p>
                        <hr>
                        <strong><i class="fas fa-user mr-1"></i> Contacto Principal</strong>
                        <p class="text-muted">' . htmlspecialchars($user_data['first_name'] . ' ' . $user_data['last_name']) . '</p>
                        <hr>
                        <strong><i class="fas fa-phone mr-1"></i> Teléfono</strong>
                        <p class="text-muted">' . htmlspecialchars($company_info['phone'] ?: 'No especificado') . '</p>
                        <hr>
                        <strong><i class="fas fa-map-marker-alt mr-1"></i> Dirección</strong>
                        <p class="text-muted">' . htmlspecialchars($company_info['address'] ?: 'No especificada') . '</p>
                        <hr>
                        <strong><i class="fas fa-envelope mr-1"></i> Email</strong>
                        <p class="text-muted">' . htmlspecialchars($user_data['email']) . '</p>
                        <hr>
                        <strong><i class="fas fa-calendar mr-1"></i> Fecha de registro</strong>
                        <p class="text-muted">' . date('d/m/Y', strtotime($user_data['created_at'])) . '</p>
                    </div>
                    <div class="col-sm-6">
                        <strong><i class="fas fa-globe mr-1"></i> Sitio Web</strong>
                        <p class="text-muted">' . ($company_info['website'] ? '<a href="' . htmlspecialchars($company_info['website']) . '" target="_blank" rel="noopener noreferrer">' . htmlspecialchars($company_info['website']) . '</a>' : 'No especificado') . '</p>
                        <hr>
                        <strong><i class="fas fa-info-circle mr-1"></i> Descripción</strong>
                        <p class="text-muted">' . ($company_info['description'] ? nl2br(htmlspecialchars($company_info['description'])) : 'No especificada') . '</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>';

include BASE_PATH . '/includes/layout.php';
?>