<?php

$base = __DIR__;

function writeFile($path, $content) {
    global $base;
    $full = $base . '/' . $path;
    $dir = dirname($full);
    if (!is_dir($dir)) mkdir($dir, 0777, true);
    file_put_contents($full, $content);
    echo "Generated: $path\n";
}

// 1. CANDIDATE SOURCING (SourcingController)
writeFile('app/Http/Controllers/Enterprise/SourcingController.php', '<?php
namespace App\Http\Controllers\Enterprise;

use App\Http\Controllers\Controller;
use App\Models\Candidate;

class SourcingController extends Controller
{
    public function index()
    {
        $candidates = Candidate::with("user")->get();
        return view("enterprise.sourcing", compact("candidates"));
    }
}');

writeFile('resources/views/enterprise/sourcing.blade.php', '@extends("layouts.app")
@section("title", "Candidate Sourcing - Signal Career Compass")
@section("page-title", "🔍 Candidate Sourcing & Talent Search Engine")

@section("content")
<div style="margin-bottom: 20px;">
    <input type="text" class="form-input" placeholder="Search candidates by skills (e.g. React, Python, Laravel), title, or location..." style="max-width: 600px;">
</div>

<div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 20px;">
    @foreach($candidates as $cand)
    <div class="card">
        <div style="display: flex; gap: 12px; align-items: center; margin-bottom: 12px;">
            <div style="width: 44px; height: 44px; border-radius: 50%; background: var(--gradient-1); display: flex; align-items: center; justify-content: center; font-weight: 700; color: white;">
                {{ strtoupper(substr($cand->user->name ?? "C", 0, 1)) }}
            </div>
            <div>
                <div style="font-weight: 700; font-size: 15px; color: var(--text-primary);">{{ $cand->user->name ?? "Candidate" }}</div>
                <div style="font-size: 12px; color: var(--primary-light);">{{ $cand->user->title ?? "Specialist" }}</div>
            </div>
        </div>

        <div style="font-size: 12px; color: var(--text-muted); margin-bottom: 12px;">
            📍 {{ $cand->user->location ?? "Remote" }} &nbsp;•&nbsp; 💰 {{ $cand->expected_salary ?? "Market Rate" }}
        </div>

        <p style="font-size: 12px; color: var(--text-secondary); line-height: 1.5; margin-bottom: 14px;">
            {{ Str::limit($cand->summary ?? "Experienced professional seeking opportunities in web engineering and enterprise technology.", 110) }}
        </p>

        @if($cand->skills)
        <div style="display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px;">
            @foreach(array_slice($cand->skills, 0, 4) as $s)
            <span style="font-size: 11px; background: rgba(56,189,248,0.1); color: #7dd3fc; padding: 2px 8px; border-radius: 4px;">
                {{ is_array($s) ? $s["name"] : $s }}
            </span>
            @endforeach
        </div>
        @endif

        <div style="border-top: 1px solid var(--border); padding-top: 10px; display: flex; justify-content: flex-end;">
            <a href="{{ route("messages") }}" class="btn btn-secondary" style="padding: 4px 12px; font-size: 12px;">💬 Send Message</a>
        </div>
    </div>
    @endforeach
</div>
@endsection');


// 2. KANBAN RECRUITMENT PIPELINE (PipelineController)
writeFile('app/Http/Controllers/Enterprise/PipelineController.php', '<?php
namespace App\Http\Controllers\Enterprise;

use App\Http\Controllers\Controller;
use App\Models\Application;

class PipelineController extends Controller
{
    public function index()
    {
        $tenantId = auth()->user()->tenant_id;
        $applications = Application::where("tenant_id", $tenantId)->with("jobPosting")->get();

        $stages = [
            "Applied" => $applications->where("status", "Applied"),
            "In Review" => $applications->where("status", "In Review"),
            "Interview Scheduled" => $applications->where("status", "Interview Scheduled"),
            "Hired" => $applications->where("status", "Hired"),
            "Rejected" => $applications->where("status", "Rejected"),
        ];

        return view("enterprise.pipeline", compact("stages"));
    }
}');

writeFile('resources/views/enterprise/pipeline.blade.php', '@extends("layouts.app")
@section("title", "Recruitment Pipeline - Signal Career Compass")
@section("page-title", "🔀 Applicant Tracking System (ATS Pipeline)")

@section("content")
<div style="display: grid; grid-template-columns: repeat(5, 1fr); gap: 16px; overflow-x: auto; min-width: 1000px;">
    @foreach($stages as $stageName => $apps)
    <div style="background: var(--bg-card); border: 1px solid var(--border); border-radius: 12px; padding: 16px;">
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; border-bottom: 1px solid var(--border); padding-bottom: 8px;">
            <div style="font-family: Outfit; font-weight: 700; font-size: 14px;">{{ $stageName }}</div>
            <span style="font-size: 12px; font-weight: 700; background: var(--bg-dark); padding: 2px 8px; border-radius: 10px; color: var(--primary-light);">
                {{ $apps->count() }}
            </span>
        </div>

        @forelse($apps as $app)
        <div style="background: var(--bg-dark); border: 1px solid var(--border); border-radius: 8px; padding: 12px; margin-bottom: 10px;">
            <div style="font-weight: 700; font-size: 13px; color: var(--text-primary);">{{ $app->applicant_name }}</div>
            <div style="font-size: 11px; color: var(--text-muted); margin-top: 2px;">{{ $app->jobPosting->title ?? $app->company }}</div>
            
            <div style="display: flex; justify-content: space-between; align-items: center; margin-top: 10px; font-size: 11px;">
                <span style="font-weight: 700; color: #38bdf8;">⚡ {{ $app->match_score }}% Match</span>
                <span style="color: var(--text-muted);">{{ $app->created_at->format("M d") }}</span>
            </div>
        </div>
        @empty
        <div style="text-align: center; padding: 20px 0; font-size: 12px; color: var(--text-muted);">
            No candidates in this stage.
        </div>
        @endforelse
    </div>
    @endforeach
</div>
@endsection');


// 3. ENTERPRISE STUDIO (TenantStudioController)
writeFile('app/Http/Controllers/Enterprise/TenantStudioController.php', '<?php
namespace App\Http\Controllers\Enterprise;

use App\Http\Controllers\Controller;
use App\Models\Tenant;
use Illuminate\Http\Request;

class TenantStudioController extends Controller
{
    public function index()
    {
        $tenant = Tenant::find(auth()->user()->tenant_id);
        return view("enterprise.studio", compact("tenant"));
    }
}');

writeFile('resources/views/enterprise/studio.blade.php', '@extends("layouts.app")
@section("title", "Enterprise Studio - Signal Career Compass")
@section("page-title", "🎨 Enterprise Partner Studio & Configuration")

@section("content")
<div style="display: grid; grid-template-columns: 2fr 1fr; gap: 20px;">
    <div class="card">
        <h3 class="card-title" style="margin-bottom: 16px;">Enterprise Workspace Settings</h3>

        <div class="form-group">
            <label class="form-label">Organization Name</label>
            <input type="text" class="form-input" value="{{ $tenant->name ?? "" }}" disabled>
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px;">
            <div class="form-group">
                <label class="form-label">Domain</label>
                <input type="text" class="form-input" value="{{ $tenant->domain ?? "" }}" disabled>
            </div>
            <div class="form-group">
                <label class="form-label">License Tier</label>
                <input type="text" class="form-input" value="{{ $tenant->license_tier ?? "Enterprise" }}" disabled>
            </div>
        </div>

        <div class="form-group">
            <label class="form-label">Theme Color (Primary Accent)</label>
            <input type="color" class="form-input" value="{{ $tenant->theme_color ?? "#0284c7" }}" style="height: 44px; padding: 4px; cursor: pointer;">
        </div>
    </div>

    <div class="card">
        <h3 class="card-title" style="margin-bottom: 16px;">Active Platform Modules</h3>
        @if($tenant && $tenant->features)
            @foreach($tenant->features as $feature => $enabled)
            <div style="display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--border);">
                <span style="font-size: 13px; text-transform: capitalize;">{{ preg_replace("/(?<!^)[A-Z]/", " $0", $feature) }}</span>
                <span class="status-badge status-{{ $enabled ? "active" : "pending" }}">{{ $enabled ? "Enabled" : "Disabled" }}</span>
            </div>
            @endforeach
        @endif
    </div>
</div>
@endsection');


// 4. MESSAGES WORKSPACE (MessageController)
writeFile('app/Http/Controllers/Shared/MessageController.php', '<?php
namespace App\Http\Controllers\Shared;

use App\Http\Controllers\Controller;
use App\Models\Conversation;
use App\Models\Message;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    public function index()
    {
        $conversations = Conversation::where("participant_1_id", auth()->id())
            ->orWhere("participant_2_id", auth()->id())
            ->get();

        return view("shared.messages", compact("conversations"));
    }
}');

writeFile('resources/views/shared/messages.blade.php', '@extends("layouts.app")
@section("title", "Messages - Signal Career Compass")
@section("page-title", "💬 1-on-1 Messaging Workspace")

@section("content")
<div class="card" style="display: grid; grid-template-columns: 280px 1fr; height: 70vh; padding: 0; overflow: hidden;">
    <!-- Conversations Sidebar -->
    <div style="border-right: 1px solid var(--border); padding: 16px; overflow-y: auto;">
        <h3 class="card-title" style="margin-bottom: 14px;">Conversations</h3>

        <div style="background: var(--bg-dark); border: 1px solid var(--border); border-radius: 8px; padding: 12px; margin-bottom: 8px; cursor: pointer;">
            <div style="font-weight: 700; font-size: 13px; color: var(--text-primary);">Sarah Jenkins</div>
            <div style="font-size: 11px; color: var(--text-muted); margin-top: 2px;">Apex Global Technologies</div>
            <div style="font-size: 11px; color: var(--primary-light); margin-top: 6px;">"We reviewed your profile!"</div>
        </div>
    </div>

    <!-- Active Thread -->
    <div style="display: flex; flex-direction: column; justify-content: space-between; background: var(--bg-dark);">
        <!-- Header -->
        <div style="padding: 16px; border-bottom: 1px solid var(--border); background: var(--bg-card);">
            <div style="font-weight: 700; font-size: 15px;">Sarah Jenkins</div>
            <div style="font-size: 11px; color: var(--text-muted);">VP of Talent & HR at Apex Global Technologies</div>
        </div>

        <!-- Chat History -->
        <div style="flex: 1; padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px;">
            <div style="align-self: flex-start; max-width: 70%; background: var(--bg-card); border: 1px solid var(--border); border-radius: 12px; padding: 12px; font-size: 13px;">
                Hi {{ auth()->user()->name }}! Thank you for applying to Apex Global Technologies. We were very impressed with your background.
            </div>

            <div style="align-self: flex-end; max-width: 70%; background: var(--gradient-1); color: white; border-radius: 12px; padding: 12px; font-size: 13px;">
                Thank you Sarah! I am very excited about the opportunity.
            </div>
        </div>

        <!-- Input Box -->
        <div style="padding: 16px; border-top: 1px solid var(--border); background: var(--bg-card); display: flex; gap: 10px;">
            <input type="text" class="form-input" placeholder="Type a message..." style="flex: 1;">
            <button class="btn btn-primary" onclick="alert(\'Message sent!\')">Send</button>
        </div>
    </div>
</div>
@endsection');


// 5. SUPER ADMIN CONTROL CENTER (SuperAdminController)
writeFile('app/Http/Controllers/Admin/SuperAdminController.php', '<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Tenant;
use App\Models\User;
use App\Models\JobPosting;
use App\Models\Application;

class SuperAdminController extends Controller
{
    public function index()
    {
        $totalTenants = Tenant::count();
        $totalUsers = User::count();
        $totalJobs = JobPosting::count();
        $totalApplications = Application::count();

        $tenants = Tenant::latest()->get();

        return view("admin.control-center", compact("totalTenants", "totalUsers", "totalJobs", "totalApplications", "tenants"));
    }
}');

writeFile('resources/views/admin/control-center.blade.php', '@extends("layouts.app")
@section("title", "Super Admin Control Center - Signal Career Compass")
@section("page-title", "🛡️ Master Super Admin Control Center")

@section("content")
<div class="stat-grid">
    <div class="stat-card">
        <div class="stat-value">{{ $totalTenants }}</div>
        <div class="stat-label">Enterprise Partners / Tenants</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ $totalUsers }}</div>
        <div class="stat-label">Registered System Users</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ $totalJobs }}</div>
        <div class="stat-label">Total Platform Vacancies</div>
    </div>
    <div class="stat-card">
        <div class="stat-value">{{ $totalApplications }}</div>
        <div class="stat-label">Total Candidate Submissions</div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h3 class="card-title">Enterprise Partner Tenant Accounts</h3>
    </div>

    <table class="data-table">
        <thead>
            <tr>
                <th>Organization Name</th>
                <th>Owner Email</th>
                <th>License Tier</th>
                <th>Monthly Revenue</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            @foreach($tenants as $t)
            <tr>
                <td>
                    <strong style="color: var(--text-primary); font-size: 15px;">{{ $t->name }}</strong><br>
                    <span style="font-size: 11px; color: var(--text-muted);">{{ $t->domain }}</span>
                </td>
                <td>{{ $t->owner_email }}</td>
                <td>{{ $t->license_tier }}</td>
                <td>${{ number_format($t->monthly_billing, 2) }}</td>
                <td>
                    <span class="status-badge status-active">{{ $t->status }}</span>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>
@endsection');

echo "\n✅ Candidate Sourcing, Pipeline ATS, Studio, Messages, and Super Admin views generated!\n";
