<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Branch;
use App\Models\Project;
use App\Models\Payment;
use App\Models\Transaction;
use Illuminate\Http\Request;

class PaymentController extends Controller
{

    public function payment_store(Request $request)
    {
        $request->validate([
            'project_id' => 'required|exists:projects,id',
            'amount' => 'required|numeric|min:0.01',
            'notes' => 'nullable|string',
        ]);

        $project = Project::findOrFail($request->project_id);

        $payment = Payment::create([
            'project_id' => $project->id,
            'user_id' => $project->user_id,
            'amount' => $request->amount,
            'payment_method' => $request->payment_method,
            'payment_date' => $request->payment_date,
            'transaction_id' => $request->transaction_id,
            'notes' => $request->notes,
            'branch_id' => $project->branch_id,
            'status' => 'unpaid',
        ]);


        return redirect()->route('admin.project.show.index', $project->id)
            ->with('success', 'Payment added successfully and invoice generated.');
    }


    public function invoice()
    {
        $payments = Payment::where('branch_id', auth()->user()->branch_id)->with('project', 'user')->orderBy('created_at', 'desc')->paginate(20);

        if (auth()->user()->role == 'admin') {
            $payments = Payment::with('project', 'user')->orderBy('created_at', 'desc')->paginate(20);
        }

        return view('admin.payments.invoice', compact('payments'));
    }


    public function view($id)
    {
        $payment = Payment::with('project', 'user')->findOrFail($id);
        $branch = Branch::find($payment->branch_id);


        return view('admin.payments.view', compact('payment', 'branch'));
    }


    public function destroy($id)
    {
        $payment = Payment::findOrFail($id);
        $payment->delete();


        return response()->json(['success' => true, 'message' => 'Payment deleted successfully.']);
    }



    public function transection()
    {

        $transections = Transaction::where('branch_id', auth()->user()->branch_id)->with('project', 'user')->orderBy('created_at', 'desc')->paginate(20);

        if (auth()->user()->role == "admin") {
            $transections = Transaction::with('project', 'user')->orderBy('created_at', 'desc')->paginate(20);
        }


        return view('admin.payments.transection', compact('transections'));
    }


    public function transection_show($id)
    {

        $transection = Transaction::with('project', 'user')->findOrFail($id);
        return view('admin.payments.transection_view', compact('transection'));
    }


    public function transection_update(Request $request, $id)
    {

        $transection = Transaction::findOrFail($id);
        $transection->status = $request->status;

        if ($request->status === 'verified') {
            $project = Project::find($transection->project_id);

            $invoice = Payment::where('project_id', $project->id)
                ->where('status', 'pending')
                ->where('user_id', $transection->user_id)
                ->first();
            $invoice->status = 'paid';
            $invoice->save();

            $project->amount_paid =  $project->amount_paid + $invoice->amount;
            $project->save();
        }


        $transection->save();

        return redirect()->route('admin.payments.transection_show', $transection->id)
            ->with('success', 'Transaction status updated successfully.');
    }
}