<?php

namespace App\Http\Controllers;

use App\Models\Customer;
use App\Models\FollowUp;
use App\Models\Lead;
use App\Models\MetaConnection;
use App\Models\Product;
use App\Models\Quotation;

class CrudController extends Controller
{
    public function customers()
    {
        $currentUser = auth()->user();
        return view('module.index', [
            'title'   => 'Customers',
            'items'   => Customer::forUser($currentUser, 'customers')->latest()->paginate(20),
            'columns' => ['name', 'phone', 'email', 'company']
        ]);
    }

    public function followups()
    {
        $currentUser = auth()->user();
        return view('module.index', [
            'title'   => 'Follow-ups',
            'items'   => FollowUp::forUser($currentUser, 'leads')->latest('due_at')->paginate(20),
            'columns' => ['subject', 'due_at', 'status', 'lead_id']
        ]);
    }

    public function products()
    {
        return view('module.index', [
            'title'   => 'Products',
            'items'   => Product::latest()->paginate(20),
            'columns' => ['name', 'sku', 'price', 'active']
        ]);
    }

    public function quotations()
    {
        $currentUser = auth()->user();
        return view('module.index', [
            'title'   => 'Quotations',
            'items'   => Quotation::forUser($currentUser, 'quotations')->latest()->paginate(20),
            'columns' => ['quote_no', 'customer_id', 'status', 'total']
        ]);
    }

    public function analytics()
    {
        $currentUser = auth()->user();
        $scopedLeads = Lead::forUser($currentUser, 'reports');

        $totalLeads = (clone $scopedLeads)->count();
        $adSpend = 0;
        $blendedCpl = $totalLeads > 0 && $adSpend > 0 ? round($adSpend / $totalLeads, 2) : 0;
        $wonDeals = (clone $scopedLeads)->where('status', 'won')->count();
        $conversionRate = $totalLeads > 0 ? round(($wonDeals / $totalLeads) * 100, 1) : 0;
        $avgResponse = '—';
        $metaConnection = MetaConnection::where('status', 'connected')->first();

        $platforms = [
            [
                'name'      => 'Meta (Facebook & Instagram)',
                'icon'      => 'meta',
                'connected' => (bool) $metaConnection,
                'spend'     => 0,
                'leads'     => (clone $scopedLeads)->whereIn('source', ['facebook', 'instagram'])->count(),
                'cpl'       => 0,
                'won'       => (clone $scopedLeads)->whereIn('source', ['facebook', 'instagram'])->where('status', 'won')->count(),
            ],
            [
                'name'      => 'Google Ads',
                'icon'      => 'google',
                'connected' => false,
                'spend'     => 0,
                'leads'     => (clone $scopedLeads)->where('source', 'google')->count(),
                'cpl'       => 0,
                'won'       => (clone $scopedLeads)->where('source', 'google')->where('status', 'won')->count(),
            ],
            [
                'name'      => 'TikTok Ads',
                'icon'      => 'tiktok',
                'connected' => false,
                'spend'     => 0,
                'leads'     => (clone $scopedLeads)->where('source', 'tiktok')->count(),
                'cpl'       => 0,
                'won'       => (clone $scopedLeads)->where('source', 'tiktok')->where('status', 'won')->count(),
            ],
            [
                'name'      => 'LinkedIn Ads',
                'icon'      => 'linkedin',
                'connected' => false,
                'spend'     => 0,
                'leads'     => (clone $scopedLeads)->where('source', 'linkedin')->count(),
                'cpl'       => 0,
                'won'       => (clone $scopedLeads)->where('source', 'linkedin')->where('status', 'won')->count(),
            ],
        ];

        $hasConnectedPlatforms = (bool) $metaConnection;

        return view('analytics', compact(
            'totalLeads',
            'adSpend',
            'blendedCpl',
            'wonDeals',
            'conversionRate',
            'avgResponse',
            'metaConnection',
            'platforms',
            'hasConnectedPlatforms'
        ));
    }
}
