<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Contracts\View\View;
use App\Models\CRM\Company;
use Illuminate\Http\Request;
use App\Models\CRM\Individual;
use App\Models\INSURANCE\Insurer;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;

class DashboardController extends Controller
{
    public function index(): View
    {
        return view('dashboard');
    }



    public function crm(): View
    {
        //$individuals = Individual::latest()->paginate(10);
        $visibleUserFields = [
            'first_name',
            'last_name',
            'email',
        ];

        // id column is displayed by default
        $idColumn = [['key' => 'id', 'label' => '#', 'visible' => true]];



        // Default columns
        $defaultColumns = collect((new Individual())->getFillable())
            ->map(fn(string $field) => [
                'key'     => $field,
                'label'   => ucfirst(str_replace('_', ' ', $field)),
                'visible' => in_array($field, $visibleUserFields),
            ])
            ->toArray();

        // Add additional or override specific columns
        $customColumns = [
            ['key' => 'created_at', 'label' => 'Joined Date', 'visible' => true],
            ['key' => 'updated_at', 'label' => 'Last Updated', 'visible' => false], // Optional hidden by default
            ['key' => 'last_contract_number', 'label' => 'Contract #', 'visible' => true],
        ];

        // Bonus: Customize Column Visibility Programmatically
        // If you want to set custom visibility rules based on user roles or permissions, add conditional logic inside your controller like this:
        // $customColumns = [
        //     ['key' => 'created_at', 'label' => 'Joined Date', 'visible' => auth()->user()->isAdmin()],
        // ];

        // Merge default columns with custom columns
        $userColumns = array_merge($idColumn, $defaultColumns, $customColumns);


        // ─────────────── Companies Columns ───────────────

        $visibleCompanyFields = [
            'short_name',
            'email',
        ];

        // 1) ID column
        $companyIdColumn = [
            ['key' => 'id', 'label' => '#', 'visible' => true],
        ];

        // 2) Default from Company fillable
        $companyDefaultColumns = collect((new Company())->getFillable())
            ->map(fn(string $field) => [
                'key'     => $field,
                'label'   => ucfirst(str_replace('_', ' ', $field)),
                'visible' => in_array($field, $visibleCompanyFields),
            ])
            ->toArray();

        // 3) Extras or overrides for companies
        $companyCustomColumns = [
            ['key' => 'created_at', 'label' => 'Created At', 'visible' => true],
            ['key' => 'updated_at', 'label' => 'Updated At', 'visible' => false],
            ['key' => 'status',     'label' => 'Status',     'visible' => true],
        ];

        $companyColumns = array_merge(
            $companyIdColumn,
            $companyDefaultColumns,
            $companyCustomColumns
        );

        // 4) Merge
        $companyColumns = collect($companyColumns)
            ->unique('key')
            ->values()
            ->toArray();


        return view('backend.crm.dashboard', compact('userColumns', 'companyColumns'));
    }

    public function osiguranje(): View
    {
        $insurers = Insurer::orderBy('short_name', 'asc')->get();
        return view('backend.insurance.dashboard', compact('insurers'));
    }
}
