<?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. SEEKER ONBOARDING PORTAL (For hired candidate Elena Rostova or any employee)
writeFile('app/Http/Controllers/Seeker/SeekerOnboardingController.php', '<?php
namespace App\Http\Controllers\Seeker;

use App\Http\Controllers\Controller;
use App\Models\OnboardingRecord;
use App\Models\TrainingVideo;
use App\Models\OnboardingChecklist;
use Illuminate\Http\Request;

class SeekerOnboardingController extends Controller
{
    public function index()
    {
        $record = OnboardingRecord::where("user_id", auth()->id())->first();
        
        $videos = [];
        $checklists = [];
        if ($record && $record->tenant_id) {
            $videos = TrainingVideo::where("tenant_id", $record->tenant_id)->get();
            $checklists = OnboardingChecklist::where("tenant_id", $record->tenant_id)->get();
        }

        return view("seeker.onboarding", compact("record", "videos", "checklists"));
    }
}');

writeFile('resources/views/seeker/onboarding.blade.php', '@extends("layouts.app")
@section("title", "My Onboarding - Signal Career Compass")
@section("page-title", "✅ Employee Onboarding & Compliance Portal")

@section("content")
@if($record)
<div style="display: grid; grid-template-columns: 2fr 1fr; gap: 20px;">
    <div>
        <!-- Progress Summary -->
        <div class="card" style="margin-bottom: 20px;">
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px;">
                <div>
                    <h3 class="card-title">Welcome to {{ $record->tenant->name ?? "Your Organization" }}</h3>
                    <div style="font-size: 13px; color: var(--text-muted); margin-top: 4px;">
                        Role: {{ $record->designation }} &nbsp;•&nbsp; Employee ID: {{ $record->employee_id }}
                    </div>
                </div>
                <span class="status-badge status-active">{{ $record->status }}</span>
            </div>

            <div style="margin-top: 16px;">
                <div style="display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 6px;">
                    <span>Overall Completion Progress</span>
                    <strong style="color: var(--primary-light);">{{ $record->progress }}%</strong>
                </div>
                <div style="height: 10px; background: var(--bg-dark); border-radius: 5px; overflow: hidden;">
                    <div style="width: {{ $record->progress }}%; height: 100%; background: var(--gradient-1);"></div>
                </div>
            </div>
        </div>

        <!-- Documents Required -->
        <div class="card" style="margin-bottom: 20px;">
            <h3 class="card-title" style="margin-bottom: 16px;">📄 Required Compliance Documents</h3>
            @if($record->documents)
                @foreach($record->documents as $doc)
                <div style="display: flex; justify-content: space-between; align-items: center; padding: 12px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 10px;">
                    <div>
                        <div style="font-weight: 600; font-size: 14px;">{{ $doc["name"] }}</div>
                        <div style="font-size: 11px; color: var(--text-muted);">{{ $doc["notes"] ?? "Required" }}</div>
                    </div>
                    <span class="status-badge status-{{ strtolower($doc["status"]) == "approved" ? "active" : "pending" }}">
                        {{ $doc["status"] }}
                    </span>
                </div>
                @endforeach
            @endif
        </div>

        <!-- Training Videos -->
        <div class="card">
            <h3 class="card-title" style="margin-bottom: 16px;">📹 Mandatory Orientation Videos</h3>
            @foreach($videos as $video)
            <div style="display: flex; justify-content: space-between; align-items: center; padding: 12px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 10px;">
                <div>
                    <div style="font-weight: 600; font-size: 14px;">{{ $video->title }}</div>
                    <div style="font-size: 11px; color: var(--text-muted);">Duration: {{ $video->duration }}</div>
                </div>
                <a href="{{ $video->url }}" target="_blank" class="btn btn-secondary" style="padding: 4px 12px; font-size: 12px;">Watch Video ▶</a>
            </div>
            @endforeach
        </div>
    </div>

    <!-- Sidebar Info -->
    <div>
        <div class="card">
            <h3 class="card-title" style="margin-bottom: 12px;">📅 Joining Date</h3>
            <div style="font-family: Outfit; font-size: 22px; font-weight: 700; color: var(--primary-light);">
                {{ $record->joining_date ? $record->joining_date->format("F d, Y") : "TBD" }}
            </div>
            <div style="font-size: 12px; color: var(--text-muted); margin-top: 8px;">
                Please ensure all documentation and orientation videos are completed prior to your start date.
            </div>
        </div>
    </div>
</div>
@else
<div class="card">
    <div class="empty-state">
        <div class="empty-state-icon">✅</div>
        <h3 class="empty-state-title">No Active Employee Onboarding</h3>
        <p class="empty-state-desc">Your onboarding portal will become active automatically once you accept an offer with an enterprise partner.</p>
        <div style="margin-top: 16px;">
            <a href="{{ route("seeker.job-feed") }}" class="btn btn-primary">Find Job Openings →</a>
        </div>
    </div>
</div>
@endif
@endsection');


// 2. SEEKER PROFILE (SeekerProfileController)
writeFile('app/Http/Controllers/Seeker/SeekerProfileController.php', '<?php
namespace App\Http\Controllers\Seeker;

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

class SeekerProfileController extends Controller
{
    public function index()
    {
        $candidate = Candidate::where("user_id", auth()->id())->first();
        return view("seeker.profile", compact("candidate"));
    }

    public function update(Request $request)
    {
        $request->validate([
            "title" => "required|string|max:255",
            "location" => "required|string",
            "summary" => "nullable|string",
            "expected_salary" => "nullable|string",
            "preferred_work_arrangement" => "nullable|string",
        ]);

        auth()->user()->update([
            "title" => $request->title,
            "location" => $request->location,
            "phone" => $request->phone,
        ]);

        Candidate::updateOrCreate(
            ["user_id" => auth()->id()],
            [
                "summary" => $request->summary,
                "about_me" => $request->about_me,
                "expected_salary" => $request->expected_salary,
                "preferred_work_arrangement" => $request->preferred_work_arrangement,
            ]
        );

        return back()->with("success", "Profile details updated successfully!");
    }
}');

writeFile('resources/views/seeker/profile.blade.php', '@extends("layouts.app")
@section("title", "My Profile - Signal Career Compass")
@section("page-title", "👤 Candidate Profile Editor")

@section("content")
<div class="card" style="max-width: 800px; margin: 0 auto;">
    <form method="POST" action="{{ route("seeker.profile.update") }}">
        @csrf
        @method("PATCH")

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px;">
            <div class="form-group">
                <label class="form-label">Full Name</label>
                <input type="text" class="form-input" value="{{ auth()->user()->name }}" disabled style="opacity: 0.7;">
            </div>
            <div class="form-group">
                <label class="form-label">Email Address</label>
                <input type="email" class="form-input" value="{{ auth()->user()->email }}" disabled style="opacity: 0.7;">
            </div>
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px;">
            <div class="form-group">
                <label class="form-label">Professional Title</label>
                <input type="text" name="title" class="form-input" value="{{ auth()->user()->title }}" placeholder="e.g. Full Stack Developer" required>
            </div>
            <div class="form-group">
                <label class="form-label">Location</label>
                <input type="text" name="location" class="form-input" value="{{ auth()->user()->location }}" placeholder="e.g. Seattle, WA" required>
            </div>
            <div class="form-group">
                <label class="form-label">Phone Number</label>
                <input type="text" name="phone" class="form-input" value="{{ auth()->user()->phone }}" placeholder="+1 (555) 000-0000">
            </div>
        </div>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px;">
            <div class="form-group">
                <label class="form-label">Expected Salary</label>
                <input type="text" name="expected_salary" class="form-input" value="{{ $candidate->expected_salary ?? "" }}" placeholder="e.g. $120,000 - $140,000">
            </div>
            <div class="form-group">
                <label class="form-label">Work Arrangement</label>
                <select name="preferred_work_arrangement" class="form-input">
                    <option value="Remote" {{ ($candidate->preferred_work_arrangement ?? "") == "Remote" ? "selected" : "" }}>Remote Only</option>
                    <option value="Hybrid" {{ ($candidate->preferred_work_arrangement ?? "") == "Hybrid" ? "selected" : "" }}>Hybrid</option>
                    <option value="Onsite" {{ ($candidate->preferred_work_arrangement ?? "") == "Onsite" ? "selected" : "" }}>On-site</option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label class="form-label">Professional Summary</label>
            <textarea name="summary" class="form-input" rows="3" placeholder="Brief summary of your technical experience and achievements...">{{ $candidate->summary ?? "" }}</textarea>
        </div>

        <div class="form-group">
            <label class="form-label">Extended Bio / About Me</label>
            <textarea name="about_me" class="form-input" rows="4" placeholder="Detailed bio, project highlights, and technical focus...">{{ $candidate->about_me ?? "" }}</textarea>
        </div>

        <div style="display: flex; justify-content: flex-end; margin-top: 20px;">
            <button type="submit" class="btn btn-primary">Save Profile Changes</button>
        </div>
    </form>
</div>
@endsection');


// 3. AI RESUME BUILDER (ResumeController)
writeFile('resources/views/seeker/resume.blade.php', '@extends("layouts.app")
@section("title", "AI Resume Builder - Signal Career Compass")
@section("page-title", "📄 AI Resume Builder & Tailoring Tool")

@section("content")
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
    <!-- Inputs -->
    <div class="card">
        <h3 class="card-title" style="margin-bottom: 16px;">✨ Resume Tailoring Engine</h3>
        
        <div class="form-group">
            <label class="form-label">Target Job Title</label>
            <input type="text" class="form-input" value="{{ auth()->user()->title }}" id="targetTitle">
        </div>

        <div class="form-group">
            <label class="form-label">Paste Target Job Description (Optional)</label>
            <textarea class="form-input" rows="5" placeholder="Paste job posting description here to auto-tune keywords for ATS algorithms..."></textarea>
        </div>

        <button class="btn btn-primary" style="width: 100%; justify-content: center;" onclick="alert(&quot;AI Resume Optimizer: Keyword alignment score 96%. Resume updated!&quot;)">⚡ Generate AI-Tailored Resume</button>
    </div>

    <!-- Preview -->
    <div class="card" style="background: #ffffff; color: #0f172a; border-radius: 8px; font-family: \'Georgia\', serif;">
        <div style="border-bottom: 2px solid #0f172a; padding-bottom: 12px; margin-bottom: 16px; text-align: center;">
            <h2 style="font-size: 24px; margin-bottom: 4px;">{{ auth()->user()->name }}</h2>
            <div style="font-size: 13px; color: #475569;">
                {{ auth()->user()->title }} &nbsp;•&nbsp; {{ auth()->user()->email }} &nbsp;•&nbsp; {{ auth()->user()->location }}
            </div>
        </div>

        <div style="margin-bottom: 16px;">
            <h4 style="font-size: 12px; text-transform: uppercase; border-bottom: 1px solid #cbd5e1; padding-bottom: 4px; margin-bottom: 8px;">Professional Overview</h4>
            <p style="font-size: 12px; line-height: 1.5; color: #334155;">
                Results-driven software professional specializing in modern scalable web application design, multi-tenant database architectures, and API integrations.
            </p>
        </div>

        <div style="margin-bottom: 16px;">
            <h4 style="font-size: 12px; text-transform: uppercase; border-bottom: 1px solid #cbd5e1; padding-bottom: 4px; margin-bottom: 8px;">Core Competencies</h4>
            <p style="font-size: 12px; color: #334155;">
                Laravel 11, PHP 8.3, MySQL Optimization, RESTful APIs, System Design, Microservices, Git.
            </p>
        </div>
    </div>
</div>
@endsection');


// 4. MOCK INTERVIEW AI (InterviewController)
writeFile('resources/views/seeker/interview.blade.php', '@extends("layouts.app")
@section("title", "Mock Interview AI - Signal Career Compass")
@section("page-title", "🎙️ AI Mock Interview Voice Simulator")

@section("content")
<div class="card" style="max-width: 700px; margin: 0 auto; text-align: center; padding: 40px;">
    <div style="width: 80px; height: 80px; background: var(--gradient-1); border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 36px; margin: 0 auto 20px;">
        🎙️
    </div>

    <h2 style="font-family: Outfit; font-size: 24px; font-weight: 700; margin-bottom: 10px;">AI Technical Interview Practice</h2>
    <p style="font-size: 14px; color: var(--text-muted); max-width: 500px; margin: 0 auto 24px;">
        Simulate a real 15-minute voice technical interview for <strong>{{ auth()->user()->title ?? "Software Engineer" }}</strong>. The AI interviewer evaluates system design, code problem solving, and communication clarity.
    </p>

    <div style="background: var(--bg-dark); border: 1px solid var(--border); border-radius: 12px; padding: 20px; text-align: left; margin-bottom: 24px;">
        <div style="font-size: 12px; color: var(--primary-light); font-weight: 700; margin-bottom: 6px;">QUESTION 1 of 5</div>
        <div style="font-size: 15px; font-weight: 600; color: var(--text-primary);">
            "How do you approach database schema migrations in a high-traffic multi-tenant SQL database to ensure zero downtime?"
        </div>
    </div>

    <button class="btn btn-primary" style="padding: 14px 32px; font-size: 15px;" onclick="alert(&quot;Recording started... Speak your response into microphone.&quot;)">
        🎙️ Start Voice Response
    </button>
</div>
@endsection');

echo "\n✅ Candidate Profile, Resume Builder, Interview AI, and Onboarding views generated!\n";
