<?php

namespace App\Http\Controllers\Settings;

use App\Http\Controllers\Controller;
use App\Models\Report;
use App\Models\ReportEntity;
use App\Models\ReportColumn;
use App\Models\ReportFilter;
use App\Models\ReportCondition;
use App\Models\ReportOrder;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ReportSettingsController extends Controller
{
    public function index()
    {
        $reports = Report::orderBy('order')->orderBy('name')->get();

        return Inertia::render('Settings/Reports/Index', [
            'reports' => $reports,
        ]);
    }

    public function edit(Report $report)
    {
        $report->load(['entities', 'columns', 'filters', 'conditions', 'orders']);

        return Inertia::render('Settings/Reports/Edit', [
            'report' => $report,
        ]);
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'slug' => 'required|string|unique:reports,slug|max:255',
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'category' => 'nullable|string|max:100',
            'enabled' => 'boolean',
            'order' => 'integer',
        ]);

        $report = Report::create($validated);

        return redirect()->route('settings.reports.index')
            ->with('success', 'Report created successfully.');
    }

    public function update(Request $request, Report $report)
    {
        $validated = $request->validate([
            'slug' => 'required|string|unique:reports,slug,' . $report->id . '|max:255',
            'name' => 'required|string|max:255',
            'description' => 'nullable|string',
            'category' => 'nullable|string|max:100',
            'enabled' => 'boolean',
            'order' => 'integer',
        ]);

        $report->update($validated);

        return redirect()->route('settings.reports.index')
            ->with('success', 'Report updated successfully.');
    }

    public function destroy(Report $report)
    {
        $report->delete();

        return redirect()->route('settings.reports.index')
            ->with('success', 'Report deleted successfully.');
    }

    public function toggleEnabled(Report $report)
    {
        $report->update(['enabled' => !$report->enabled]);

        return back()->with('success', 'Report status updated.');
    }

    // Entity CRUD
    public function storeEntity(Request $request, Report $report)
    {
        $validated = $request->validate([
            'model_class' => 'required|string|max:255',
            'alias' => 'nullable|string|max:50',
            'join_type' => 'required|in:base,join,leftJoin,rightJoin',
            'join_first' => 'nullable|string|max:100',
            'join_operator' => 'nullable|string|max:10',
            'join_second' => 'nullable|string|max:100',
            'order' => 'integer',
        ]);

        $report->entities()->create($validated);

        return back()->with('success', 'Entity added successfully.');
    }

    public function updateEntity(Request $request, ReportEntity $entity)
    {
        $validated = $request->validate([
            'model_class' => 'required|string|max:255',
            'alias' => 'nullable|string|max:50',
            'join_type' => 'required|in:base,join,leftJoin,rightJoin',
            'join_first' => 'nullable|string|max:100',
            'join_operator' => 'nullable|string|max:10',
            'join_second' => 'nullable|string|max:100',
            'order' => 'integer',
        ]);

        $entity->update($validated);

        return back()->with('success', 'Entity updated successfully.');
    }

    public function destroyEntity(ReportEntity $entity)
    {
        $entity->delete();

        return back()->with('success', 'Entity deleted successfully.');
    }

    // Column CRUD
    public function storeColumn(Request $request, Report $report)
    {
        $validated = $request->validate([
            'key' => 'required|string|max:100',
            'label' => 'required|string|max:255',
            'type' => 'required|string|max:50',
            'expression' => 'required|string',
            'sortable' => 'boolean',
            'visible' => 'boolean',
            'order' => 'integer',
            'format_options' => 'nullable|array',
        ]);

        $report->columns()->create($validated);

        return back()->with('success', 'Column added successfully.');
    }

    public function updateColumn(Request $request, ReportColumn $column)
    {
        $validated = $request->validate([
            'key' => 'required|string|max:100',
            'label' => 'required|string|max:255',
            'type' => 'required|string|max:50',
            'expression' => 'required|string',
            'sortable' => 'boolean',
            'visible' => 'boolean',
            'order' => 'integer',
            'format_options' => 'nullable|array',
        ]);

        $column->update($validated);

        return back()->with('success', 'Column updated successfully.');
    }

    public function destroyColumn(ReportColumn $column)
    {
        $column->delete();

        return back()->with('success', 'Column deleted successfully.');
    }

    // Filter CRUD
    public function storeFilter(Request $request, Report $report)
    {
        $validated = $request->validate([
            'key' => 'required|string|max:100',
            'label' => 'required|string|max:255',
            'type' => 'required|string|max:50',
            'nullable' => 'boolean',
            'default_value' => 'nullable|string',
            'options' => 'nullable|array',
            'data_source' => 'nullable|string|max:255',
            'order' => 'integer',
        ]);

        $report->filters()->create($validated);

        return back()->with('success', 'Filter added successfully.');
    }

    public function updateFilter(Request $request, ReportFilter $filter)
    {
        $validated = $request->validate([
            'key' => 'required|string|max:100',
            'label' => 'required|string|max:255',
            'type' => 'required|string|max:50',
            'nullable' => 'boolean',
            'default_value' => 'nullable|string',
            'options' => 'nullable|array',
            'data_source' => 'nullable|string|max:255',
            'order' => 'integer',
        ]);

        $filter->update($validated);

        return back()->with('success', 'Filter updated successfully.');
    }

    public function destroyFilter(ReportFilter $filter)
    {
        $filter->delete();

        return back()->with('success', 'Filter deleted successfully.');
    }

    // Condition CRUD
    public function storeCondition(Request $request, Report $report)
    {
        $validated = $request->validate([
            'column' => 'required|string|max:255',
            'operator' => 'required|string|max:50',
            'value_type' => 'required|in:static,filter,expression',
            'value' => 'nullable|string',
            'filter_key' => 'nullable|string|max:100',
            'logical_operator' => 'required|in:AND,OR',
            'group_id' => 'nullable|integer',
            'order' => 'integer',
            'enabled' => 'boolean',
        ]);

        $report->conditions()->create($validated);

        return back()->with('success', 'Condition added successfully.');
    }

    public function updateCondition(Request $request, ReportCondition $condition)
    {
        $validated = $request->validate([
            'column' => 'required|string|max:255',
            'operator' => 'required|string|max:50',
            'value_type' => 'required|in:static,filter,expression',
            'value' => 'nullable|string',
            'filter_key' => 'nullable|string|max:100',
            'logical_operator' => 'required|in:AND,OR',
            'group_id' => 'nullable|integer',
            'order' => 'integer',
            'enabled' => 'boolean',
        ]);

        $condition->update($validated);

        return back()->with('success', 'Condition updated successfully.');
    }

    public function destroyCondition(ReportCondition $condition)
    {
        $condition->delete();

        return back()->with('success', 'Condition deleted successfully.');
    }

    // Order CRUD
    public function storeOrder(Request $request, Report $report)
    {
        $validated = $request->validate([
            'column' => 'required|string|max:255',
            'direction' => 'required|in:ASC,DESC',
            'order' => 'integer',
        ]);

        $report->orders()->create($validated);

        return back()->with('success', 'Order clause added successfully.');
    }

    public function updateOrder(Request $request, ReportOrder $order)
    {
        $validated = $request->validate([
            'column' => 'required|string|max:255',
            'direction' => 'required|in:ASC,DESC',
            'order' => 'integer',
        ]);

        $order->update($validated);

        return back()->with('success', 'Order clause updated successfully.');
    }

    public function destroyOrder(ReportOrder $order)
    {
        $order->delete();

        return back()->with('success', 'Order clause deleted successfully.');
    }
}
