<?php

namespace App\Http\Controllers;

use App\Models\Agency;
use App\Models\Campaign;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CampaignController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $search =  $request->input('q');
        if(isset($search) && !empty($search)){
            $campaigns = Campaign::where(function ($query) use ($search) {
                $query->where('name', 'like', $search.'%');              
                    // ->orWhere('email', 'like', $search.'%')
                    // ->orWhere('mobile', 'like', $search.'%');
            })->paginate(15);
           $campaigns->appends(['q' => $search]);
        } else {
            $campaigns = Campaign::paginate(15);
        }

        return view('campaigns.index', compact('campaigns', 'search'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $agencies = Agency::all();
        return view('campaigns.create', compact('agencies'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' =>'required',
            'enabled' =>'required',
        ]);

        if(isset($request->agency_id) && empty($request->agency_id)) {
            $agency = Agency::find($request->agency_id);
        } else {
            $agency = Agency::where('user_id', '=', auth()->user()->id)->first(); 
        }
        
        $campaign = new Campaign([
            'name' => $request->get('name'),
            'description' => $request->get('description'),
            'enabled' => $request->get('enabled'),
            'agency_id' => $agency->id
        ]); 

        $campaign->save();
        return redirect('/campaigns')->with('success', 'Campaign Added!');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Campaign  $campaign
     * @return \Illuminate\Http\Response
     */
    public function show(Campaign $campaign)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Campaign  $campaign
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $campaign = Campaign::find($id);
        $agencies = Agency::all();
        $selectedAgency = $campaign->agency()->pluck('id')->first();
        return view('campaigns.edit', compact('campaign', 'agencies', 'selectedAgency'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Campaign  $campaign
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'name' =>'required',
            'enabled' =>'required'
        ]);

        
        if(isset($request->agency_id) && !empty($request->agency_id)) {
            $agency = Agency::find($request->agency_id);
        } else {
            $agency = Agency::where('user_id', '=', auth()->user()->id)->first(); 
        }

        $campaign = Campaign::find($id);
        $campaign->name =  $request->get('name');
        $campaign->description =  $request->get('description');
        $campaign->enabled = $request->get('enabled');
        $campaign->agency_id = $agency->id;
        $campaign->save();

        return redirect('/campaigns')->with('success', 'Campaign updated!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Campaign  $campaign
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $campaign = Campaign::find($id);
        $campaign->delete();

        return redirect('/campaigns')->with('success', 'Campaign deleted!');
    }
}
