@extends('layouts.admin')

@section('title')
    ModpackChecker
@endsection

@section('content-header')
<h1>ModpackChecker<small>4-Platform Modpack Version Monitoring</small></h1>
@endsection

@section('content')

<style>
.callout.callout-info {
    background: #1a1a2e !important;
    border-color: #00b4d8;
    color: #ccc;
}
.callout.callout-info h4 { color: #00b4d8; }
.callout.callout-info a { color: #00b4d8; text-decoration: underline; }

.callout.callout-warning {
    background: #1a1a2e !important;
    border-color: #f59e0b;
    color: #ccc;
}
.callout.callout-warning h4 { color: #f59e0b; }
.callout.callout-warning a { color: #f59e0b; text-decoration: underline; }

.callout.callout-danger {
    background: #1a1a2e !important;
    border-color: #e74c3c;
    color: #ccc;
}
.callout.callout-danger h4 { color: #e74c3c; }
</style>

{{-- License Status Banners --}}
@if($license['status'] === 'expired')
<div class="row">
    <div class="col-xs-12">
        <div class="callout callout-danger">
            <h4><i class="fa fa-exclamation-triangle"></i> License Expired</h4>
            <p>Your license could not be validated. Pro features are disabled. Please check your order ID or contact support.</p>
        </div>
    </div>
</div>
@elseif($license['status'] === 'grace')
<div class="row">
    <div class="col-xs-12">
        <div class="callout callout-warning">
            <h4><i class="fa fa-clock-o"></i> License Validation Warning</h4>
            <p>Could not reach license server. Grace period expires: <strong>{{ $license['grace_expires'] }}</strong></p>
        </div>
    </div>
</div>
@endif

<form action="{{ $root }}" method="POST">
    {{ csrf_field() }}

    <div class="row">
        {{-- License Activation --}}
        <div class="col-xs-12 col-md-6">
            <div class="box box-warning">
                <div class="box-header with-border">
                    <h3 class="box-title">
                        <i class="fa fa-id-card"></i> License
                    </h3>
                    <div class="box-tools pull-right">
                        @if($license['status'] === 'active')
                            <span class="label label-success">Active</span>
                        @elseif($license['status'] === 'grace')
                            <span class="label label-warning">Grace Period</span>
                        @elseif($license['status'] === 'expired')
                            <span class="label label-danger">Expired</span>
                        @else
                            <span class="label label-default">Not Activated</span>
                        @endif
                    </div>
                </div>
                <div class="box-body">
                    <div class="form-group">
                        <label class="control-label">BuiltByBit Order ID</label>
                        <input type="text" name="order_id" value="{{ $license['order_id'] }}"
                               placeholder="Enter your order ID..." class="form-control" />
                        <p class="text-muted small">Enter the order ID from your BuiltByBit purchase to activate.</p>
                    </div>
                    @if($license['status'] === 'active' || $license['status'] === 'grace')
                    <div class="well well-sm">
                        <strong>Tier:</strong> {{ ucfirst($license['tier']) }}
                        &nbsp;|&nbsp;
                        <strong>Last Validated:</strong> {{ $license['last_validated'] ?: 'Never' }}
                    </div>
                    @endif
                </div>
                @if(!empty($license['order_id']))
                <div class="box-footer">
                    <button type="submit" name="deactivate_license" value="1" class="btn btn-danger btn-xs"
                            onclick="return confirm('Are you sure? This frees the activation slot.')">
                        Deactivate License
                    </button>
                </div>
                @endif
            </div>
        </div>

        {{-- CurseForge API Key --}}
        <div class="col-xs-12 col-md-6">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title"><i class="fa fa-key"></i> CurseForge API Key</h3>
                </div>
                <div class="box-body">
                    <div class="form-group">
                        <label class="control-label">API Key (BYOK)</label>
                        <input type="password" name="curseforge_api_key" value="{{ $curseforge_api_key }}"
                               placeholder="$2a$10$..." class="form-control" autocomplete="off" />
                        <p class="text-muted small">
                            Get your free API key from
                            <a href="https://console.curseforge.com/" target="_blank">console.curseforge.com</a>.
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        {{-- Check Interval (PRO) --}}
        <div class="col-xs-12 col-md-6">
            <div class="box box-info">
                <div class="box-header with-border">
                    <h3 class="box-title">
                        <i class="fa fa-clock-o"></i> Check Interval
                        <span class="label label-warning">PRO</span>
                    </h3>
                </div>
                <div class="box-body">
                    <div class="form-group">
                        <label class="control-label">Automatic Check Frequency</label>
                        <select class="form-control" name="check_interval"
                            {{ $license['tier'] !== 'professional' || $license['status'] === 'expired' ? 'disabled' : '' }}>
                            <option value="daily" {{ $check_interval === 'daily' ? 'selected' : '' }}>Daily (24 Hours)</option>
                            <option value="12h" {{ $check_interval === '12h' ? 'selected' : '' }}>Every 12 Hours</option>
                            <option value="6h" {{ $check_interval === '6h' ? 'selected' : '' }}>Every 6 Hours</option>
                        </select>
                        <p class="text-muted small">
                            @if($license['tier'] === 'professional' && $license['status'] !== 'expired')
                                Professional tier — custom check intervals enabled.
                            @else
                                Upgrade to Professional for more frequent automated checks.
                            @endif
                        </p>
                    </div>
                </div>
            </div>
        </div>

        {{-- Discord Webhook (PRO) --}}
        <div class="col-xs-12 col-md-6">
            <div class="box box-success">
                <div class="box-header with-border">
                    <h3 class="box-title">
                        <i class="fa fa-bell"></i> Discord Notifications
                        <span class="label label-warning">PRO</span>
                    </h3>
                </div>
                <div class="box-body">
                    <div class="form-group">
                        <label class="control-label">Webhook URL</label>
                        <input type="url" name="discord_webhook_url" value="{{ $discord_webhook_url }}"
                               placeholder="https://discord.com/api/webhooks/..."
                               class="form-control"
                               {{ $license['tier'] !== 'professional' || $license['status'] === 'expired' ? 'disabled' : '' }} />
                        <p class="text-muted small">
                            @if($license['tier'] === 'professional' && $license['status'] !== 'expired')
                                Professional tier — Discord webhook alerts enabled.
                            @else
                                Upgrade to Professional for Discord update alerts.
                            @endif
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        {{-- Supported Platforms --}}
        <div class="col-xs-12 col-md-6">
            <div class="box box-default">
                <div class="box-header with-border">
                    <h3 class="box-title"><i class="fa fa-check-circle"></i> Supported Platforms</h3>
                </div>
                <div class="box-body no-padding">
                    <table class="table">
                        <tr>
                            <td><i class="fa fa-fire text-red"></i> <strong>CurseForge</strong></td>
                            <td class="text-muted">Requires API Key</td>
                        </tr>
                        <tr>
                            <td><i class="fa fa-leaf text-green"></i> <strong>Modrinth</strong></td>
                            <td class="text-muted">No key required</td>
                        </tr>
                        <tr>
                            <td><i class="fa fa-cogs text-blue"></i> <strong>Technic</strong></td>
                            <td class="text-muted">No key required</td>
                        </tr>
                        <tr>
                            <td><i class="fa fa-cube text-red"></i> <strong>FTB</strong></td>
                            <td>
                                @if($license['tier'] === 'professional')
                                    <span class="text-green">Enabled</span>
                                @else
                                    <span class="label label-warning">PRO</span>
                                @endif
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        {{-- Update Available --}}
        @if(!empty($license['latest_version']) && $license['latest_version'] !== '1.0.0')
        <div class="col-xs-12 col-md-6">
            <div class="box box-warning">
                <div class="box-header with-border">
                    <h3 class="box-title"><i class="fa fa-download"></i> Update Available</h3>
                </div>
                <div class="box-body">
                    <p>Version <strong>{{ $license['latest_version'] }}</strong> is available.</p>
                    <a href="https://builtbybit.com/" target="_blank" class="btn btn-warning btn-sm">
                        Download from BuiltByBit
                    </a>
                </div>
            </div>
        </div>
        @endif
    </div>

    {{-- How It Works --}}
    <div class="row">
        <div class="col-xs-12">
            <div class="callout callout-info">
                <h4><i class="fa fa-info-circle"></i> How It Works</h4>
                <p>
                    ModpackChecker automatically detects modpacks via Egg Variables or file fingerprinting.
                    Set <code>MODPACK_PLATFORM</code> and <code>MODPACK_ID</code> in your server's startup
                    variables for the most reliable detection.
                </p>
            </div>
        </div>
    </div>

    {{-- Save Button --}}
    <div class="row">
        <div class="col-xs-12">
            <button type="submit" name="_method" value="PATCH" class="btn btn-primary">
                <i class="fa fa-save"></i> Save Changes
            </button>
        </div>
    </div>

    {{-- Support --}}
    <div class="row" style="margin-top: 20px;">
        <div class="col-xs-12">
            <div class="callout callout-warning">
                <h4><i class="fa fa-life-ring"></i> Need Help?</h4>
                <p>Join our Discord for support: <a href="https://firefrostgaming.com/discord" target="_blank">firefrostgaming.com/discord</a></p>
            </div>
        </div>
    </div>
</form>

@endsection
