<?php

namespace App\Http\Controllers;

use App\Models\BonusSetup;
use Illuminate\Http\Request;

class BonusSetupController extends Controller
{
    public function index()
    {
        $bonusSetups = BonusSetup::latest()->get();
        return view('bonus_setups.index', compact('bonusSetups'));
    }

    public function create()
    {
        $bonusSetup = new BonusSetup();
        return view('bonus_setups._form', compact('bonusSetup'));
    }

    public function store(Request $request)
    {

        $data = $request->validate([
            'name' => 'required|string|max:255',
            'bonus_type' => 'required|string',
            'calculation_type' => 'required|string',
            'flat_amount' => 'nullable|numeric',
            'target_percent' => 'nullable|numeric',
            'max_percent' => 'nullable|numeric',
            'formula' => 'nullable|string',
            'notes' => 'nullable|string',
            'active' => 'nullable|boolean',
            'effective_start' => 'nullable|date',
            'effective_end' => 'nullable|date',
        ]);

        // Store conditional values
        if ($data['calculation_type'] !== 'flat') {
            $data['flat_amount'] = null;
        }
        if ($data['calculation_type'] !== 'percentage') {
            $data['target_percent'] = null;
            $data['max_percent'] = null;
        }
        if ($data['calculation_type'] !== 'formula') {
            $data['formula'] = null;
        }

        // $data['organisation_id'] = auth()->user()->organisation_id; // set org if required

        BonusSetup::create($data);

        return redirect()->route('bonus-setups.index')->with('success', 'Bonus setup created successfully.');
    }

    public function edit(BonusSetup $bonusSetup)
    {
        return view('bonus_setups._form', compact('bonusSetup'));
    }

    public function update(Request $request, BonusSetup $bonusSetup)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'bonus_type' => 'required|string',
            'calculation_type' => 'required|string',
            'flat_amount' => 'nullable|numeric',
            'target_percent' => 'nullable|numeric',
            'max_percent' => 'nullable|numeric',
            'formula' => 'nullable|string',
            'notes' => 'nullable|string',
            'active' => 'nullable|boolean',
            'effective_start' => 'nullable|date',
            'effective_end' => 'nullable|date',
        ]);

        if ($data['calculation_type'] !== 'flat') {
            $data['flat_amount'] = null;
        }
        if ($data['calculation_type'] !== 'percentage') {
            $data['target_percent'] = null;
            $data['max_percent'] = null;
        }
        if ($data['calculation_type'] !== 'formula') {
            $data['formula'] = null;
        }

        // return $data;
        $bonusSetup->update($data);

        return redirect()->route('bonus-setups.index')->with('success', 'Bonus setup updated successfully.');
    }

    public function destroy(BonusSetup $bonusSetup)
    {
        $bonusSetup->delete();
        return redirect()->route('bonus-setups.index')->with('success', 'Bonus setup deleted.');
    }
}
