<?php

$resources = [
    'destinations' => ['name', 'slug', 'is_featured'],
    'testimonials' => ['name', 'rating', 'is_active'],
    'posts' => ['title', 'type', 'is_active'],
    'features' => ['title', 'icon', 'is_active'],
];

$baseDir = __DIR__ . '/resources/views/admin';

foreach ($resources as $resource => $fields) {
    $dir = $baseDir . '/' . $resource;
    if (!is_dir($dir)) mkdir($dir, 0777, true);
    
    $modelName = ucfirst(rtrim($resource, 's'));
    $varName = rtrim($resource, 's');

    // Index View
    $index = "@extends('layouts.admin')\n@section('title', '$modelName Management')\n@section('content')\n";
    $index .= "<div class=\"flex justify-between items-center mb-6\">\n<h1 class=\"text-2xl font-semibold text-gray-800\">$modelName Management</h1>\n<a href=\"{{ route('admin.{$resource}.create') }}\" class=\"bg-blue-600 text-white px-4 py-2 rounded-lg\">Add $modelName</a>\n</div>\n";
    $index .= "<div class=\"bg-white rounded-lg shadow overflow-hidden\">\n<table class=\"min-w-full\">\n<thead class=\"bg-gray-50\">\n<tr>\n";
    foreach ($fields as $field) {
        $index .= "<th class=\"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase\">" . ucfirst(str_replace('_', ' ', $field)) . "</th>\n";
    }
    $index .= "<th class=\"px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase\">Actions</th>\n</tr>\n</thead>\n<tbody class=\"divide-y divide-gray-200\">\n";
    $index .= "@foreach(\$$resource as \$$varName)\n<tr>\n";
    foreach ($fields as $field) {
        $index .= "<td class=\"px-6 py-4 whitespace-nowrap\">{{ \${$varName}->{$field} }}</td>\n";
    }
    $index .= "<td class=\"px-6 py-4 whitespace-nowrap text-right\">\n<a href=\"{{ route('admin.{$resource}.edit', \${$varName}) }}\" class=\"text-blue-600 hover:text-blue-900 mr-3\">Edit</a>\n";
    $index .= "<form action=\"{{ route('admin.{$resource}.destroy', \${$varName}) }}\" method=\"POST\" class=\"inline-block\" onsubmit=\"return confirm('Are you sure?')\">\n@csrf\n@method('DELETE')\n<button type=\"submit\" class=\"text-red-600 hover:text-red-900\">Delete</button>\n</form>\n</td>\n</tr>\n@endforeach\n";
    $index .= "</tbody>\n</table>\n<div class=\"p-4\">\n{{ \${$resource}->links() }}\n</div>\n</div>\n@endsection";
    
    file_put_contents("$dir/index.blade.php", $index);

    // Create View (simplified)
    $create = "@extends('layouts.admin')\n@section('title', 'Create $modelName')\n@section('content')\n";
    $create .= "<div class=\"max-w-2xl mx-auto bg-white p-6 rounded-lg shadow\">\n<h1 class=\"text-2xl font-semibold mb-6\">Create $modelName</h1>\n";
    $create .= "<form action=\"{{ route('admin.{$resource}.store') }}\" method=\"POST\" enctype=\"multipart/form-data\">\n@csrf\n";
    foreach ($fields as $field) {
        if (str_contains($field, 'is_')) {
            $create .= "<div class=\"mb-4\">\n<label class=\"flex items-center\">\n<input type=\"checkbox\" name=\"$field\" value=\"1\" class=\"rounded border-gray-300\">\n<span class=\"ml-2 text-sm text-gray-600\">" . ucfirst(str_replace('_', ' ', $field)) . "</span>\n</label>\n</div>\n";
        } else {
            $create .= "<div class=\"mb-4\">\n<label class=\"block text-sm font-medium text-gray-700 mb-2\">" . ucfirst($field) . "</label>\n<input type=\"text\" name=\"$field\" class=\"w-full rounded-md border-gray-300 shadow-sm\">\n</div>\n";
        }
    }
    // generic image input
    if ($resource !== 'features') {
        $create .= "<div class=\"mb-4\">\n<label class=\"block text-sm font-medium text-gray-700 mb-2\">Image</label>\n<input type=\"file\" name=\"image\" class=\"w-full\">\n</div>\n";
    }
    $create .= "<button type=\"submit\" class=\"w-full bg-blue-600 text-white px-4 py-2 rounded-lg\">Save</button>\n</form>\n</div>\n@endsection";
    
    file_put_contents("$dir/create.blade.php", $create);

    // Edit View (simplified)
    $edit = "@extends('layouts.admin')\n@section('title', 'Edit $modelName')\n@section('content')\n";
    $edit .= "<div class=\"max-w-2xl mx-auto bg-white p-6 rounded-lg shadow\">\n<h1 class=\"text-2xl font-semibold mb-6\">Edit $modelName</h1>\n";
    $edit .= "<form action=\"{{ route('admin.{$resource}.update', \${$varName}) }}\" method=\"POST\" enctype=\"multipart/form-data\">\n@csrf\n@method('PUT')\n";
    foreach ($fields as $field) {
        if (str_contains($field, 'is_')) {
            $edit .= "<div class=\"mb-4\">\n<label class=\"flex items-center\">\n<input type=\"checkbox\" name=\"$field\" value=\"1\" {{ \${$varName}->{$field} ? 'checked' : '' }} class=\"rounded border-gray-300\">\n<span class=\"ml-2 text-sm text-gray-600\">" . ucfirst(str_replace('_', ' ', $field)) . "</span>\n</label>\n</div>\n";
        } else {
            $edit .= "<div class=\"mb-4\">\n<label class=\"block text-sm font-medium text-gray-700 mb-2\">" . ucfirst($field) . "</label>\n<input type=\"text\" name=\"$field\" value=\"{{ \${$varName}->{$field} }}\" class=\"w-full rounded-md border-gray-300 shadow-sm\">\n</div>\n";
        }
    }
    if ($resource !== 'features') {
        $edit .= "<div class=\"mb-4\">\n<label class=\"block text-sm font-medium text-gray-700 mb-2\">Image</label>\n<input type=\"file\" name=\"image\" class=\"w-full\">\n</div>\n";
    }
    $edit .= "<button type=\"submit\" class=\"w-full bg-blue-600 text-white px-4 py-2 rounded-lg\">Update</button>\n</form>\n</div>\n@endsection";
    
    file_put_contents("$dir/edit.blade.php", $edit);
}

echo "Views generated successfully.\n";
