<?php

namespace App\Modules\FINANCE\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\FINANCE\Models\Subscription;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;
use Illuminate\Support\Facades\DB;

class SubscriptionController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $query = Subscription::with(['client', 'plan']);

            return DataTables::of($query)
                ->addIndexColumn()
                ->addColumn('client', fn($row) => $row->client?->name ?? '-')
                ->addColumn('plan', fn($row) => $row->plan?->name ?? '-')
                ->editColumn('start_date', fn($row) => $row->start_date?->format('d M Y') ?? '-')
                ->editColumn('next_billing_date', fn($row) => $row->next_billing_date?->format('d M Y') ?? '-')
                ->editColumn('status', function($row) {
                    $class = $row->status === 'active' ? 'bg-success' : 'bg-secondary';
                    return '<span class="badge '.$class.'">'.ucfirst($row->status).'</span>';
                })
                ->addColumn('action', function($row) {
                    return view('finance::subscriptions.partials.actions', compact('row'))->render();
                })
                ->rawColumns(['status', 'action'])
                ->make(true);
        }

        return view('finance::subscriptions.index');
    }

    public function create()
    {
        $clients = \App\Modules\FINANCE\Models\Client::all();
        $plans = DB::table('finance_subscription_plans')->where('status', 'active')->get();
        return view('finance::subscriptions.create', compact('clients', 'plans'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'client_id' => 'required',
            'plan_id'   => 'required',
            'start_date' => 'required|date',
        ]);

        $data = $request->all();
        $data['company_id'] = 1;
        Subscription::create($data);

        return response()->json([
            'status'  => true,
            'message' => 'Subscription started successfully'
        ]);
    }

    public function show(Subscription $subscription)
    {
        return view('finance::subscriptions.show', compact('subscription'));
    }

    public function edit(Subscription $subscription)
    {
        $clients = \App\Modules\FINANCE\Models\Client::all();
        $plans = DB::table('finance_subscription_plans')->where('status', 'active')->get();
        return view('finance::subscriptions.edit', compact('subscription', 'clients', 'plans'));
    }

    public function update(Request $request, Subscription $subscription)
    {
        $subscription->update($request->all());

        return response()->json([
            'status'  => true,
            'message' => 'Subscription updated successfully'
        ]);
    }

    public function destroy(Subscription $subscription)
    {
        $subscription->delete();
        return response()->json(['status' => true, 'message' => 'Subscription deleted successfully']);
    }
}
