<?php

namespace App\Http\Controllers;

use App\Exports\ReportExport;
use App\Http\Resources\ConfigResource;
use App\Http\Resources\ReportsResource;
use App\Models\Material;
use App\Models\Material_group;
use App\Models\Material_type;
use App\Models\Report_config;
use App\Models\Stock_take_material;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class ReportConfigController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('reports.configs.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('reports.configs.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            $data = [
                'type' => $request->type,
                'type_code' => $request->type_code
            ];
            $check = Report_config::where([['type', $request->type], ['type_code', $request->type_code]])->first();
            if ($check) {
                $res = [
                    'status' => 500,
                    'message' => 'Report configuration already exists!'
                ];
                return response()->json($res, 500);
            }
            Report_config::create(array_merge($data));
            $res = [
                'status' => 200,
                'message' => 'Report configuration saved successfully!'
            ];
            return response()->json($res, 200);
        } catch (\Throwable $th) {
            $res = [
                'status' => 500,
                'message' => $th->getMessage()
            ];
            return response()->json($res, 500);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        try {
            switch ($id) {
                case 'type':
                    $response = Material_type::all();
                    # code...
                    break;
                case 'group':
                    $response = Material_group::all();
                    break;
                default:
                    $response = Material::all();
                    # code...
                    break;
            }
            $res = [
                'status' => 200,
                'response' => $response
            ];
            return response()->json($res, 200);
        } catch (\Throwable $th) {
            $res = [
                'status' => 500,
                'response' => $th->getMessage()
            ];
            return response()->json($res, 500);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function preview_filter(Request $request)
    {
        try {
            // $q->whereDate('created_at', '=', date('Y-m-d'));
            $type = $request->type;
            $type_code = $request->type_code;
            $from = $request->from;
            $to = $request->to;
            $stocks = [];
            switch ($type) {
                case 'type':
                    $stocks = Stock_take_material::where('material_type_code', $type_code)->whereDate('created_at', '<=', $to)->whereDate('created_at', '>=', $from)->get();
                    break;
                case 'group':
                    $stocks = Stock_take_material::where('material_group_code', $type_code)->whereDate('created_at', '<=', $to)->whereDate('created_at', '>=', $from)->get();
                    break;
                case 'material':
                    $stocks = Stock_take_material::where('material_no', $type_code)->whereDate('created_at', '<=', $to)->whereDate('created_at', '>=', $from)->get();
                    break;
                default:
                    # code...
                    break;
            }
            $res = [
                'status' => 200,
                'stocks' => ReportsResource::collection($stocks),
            ];
            return response()->json($res, 200);
        } catch (\Throwable $th) {
            $res = [
                'status' => 500,
                'stocks' => $th->getMessage(),
            ];
            return response()->json($res, 500);
        }
    }

    public function fetch_configs()
    {
        try {
            $types = Report_config::where('type', 'type')->with('type')->latest()->get();
            $groups = Report_config::where('type', 'group')->with('group')->latest()->get();
            $materials = Report_config::where('type', 'material')->with('material')->latest()->get();
            $res = [
                'status' => 200,
                'types' => ConfigResource::collection($types),
                'groups' => ConfigResource::collection($groups),
                'materials' => ConfigResource::collection($materials)
            ];
            return response()->json($res, 200);
        } catch (\Throwable $th) {
            $res = [
                'status' => 500,
                'message' => $th->getMessage()
            ];
            return response()->json($res, 500);
        }
    }

    public function export($id, $from, $to)
    {
        $report = Report_config::where('id', $id)->first();
        $report_name = $this->get_report_name($report->type, $report->type_code);
        $stocks = [];
        switch ($report->type) {
            case 'type':
                $stocks = Stock_take_material::where('material_type_code', $report->type_code)->whereDate('created_at', '<=', $to)->whereDate('created_at', '>=', $from)->get();
                break;
            case 'group':
                $stocks = Stock_take_material::where('material_group_code', $report->type_code)->whereDate('created_at', '<=', $to)->whereDate('created_at', '>=', $from)->get();
                break;
            case 'material':
                $stocks = Stock_take_material::where('material_no', $report->type_code)->whereDate('created_at', '<=', $to)->whereDate('created_at', '>=', $from)->get();
                break;
            default:
                # code...
                break;
        }
        $data = ReportsResource::collection($stocks);
        // return $data;
        return Excel::download(new ReportExport($data), '' . $report_name . '.xlsx');
    }

    private function get_report_name($type, $code)
    {
        switch ($type) {
            case 'type':
                $type = Material_type::where('material_type_code', $code)->first();
                if ($type) {
                    return $type->material_type_name . " Report";
                }
                return "";
                break;
            case 'group':
                $group = Material_group::where('material_group_code', $code)->first();
                if ($group) {
                    return $group->material_group_name . " Report";
                }
                return "";
                break;
            case 'material':
                $mat = Material::where('material_no', $code)->first();
                if ($mat) {
                    return $mat->material_name . " Report";
                }
                return "";
                break;
            default:
                # code...
                break;
        }
    }
}