<?php
namespace App\Repositories;

use App\Models\Project;
use App\Repositories\Interfaces\Projectinterface;
use App\Traits\FileUploadTrait;
use Illuminate\Http\Request;
use Log;

class ProjectRepository implements Projectinterface
{

    use FileUploadTrait;
    public function list()
    {
        $list = Project::select(
            'id',
            'name',
            'img1',
            'is_delete'
        )
            ->where('is_delete', 0)
            ->orderBy('id', 'DESC')
            ->get();

        $baseImgPath = $this->getBasePath();

        $list->map(function ($item) use ($baseImgPath) {
            $item['img1']  = $item->img1 ? $baseImgPath . $item->img1 : null;
        });

        return $list;
    }

    public function find($id)
    {
        $course = Project::find($id);
        if (!$course) {
            return null; // or handle the not found case as needed
        }
        $baseImgPath = $this->getBasePath();
        $course['img1'] = $course->img1 ? $baseImgPath . $course->img1 : null;
        $course['img2'] = $course->img2 ? $baseImgPath . $course->img2 : null;
        $course['img3'] = $course->img3 ? $baseImgPath . $course->img3 : null;
        $course['img4'] = $course->img4 ? $baseImgPath . $course->img4 : null;
        Log::info($course);

        return $course;
    }


    public function store(Request $request, $img1, $img2, $img3, $img4)
    {
        $course            = new Project;
        $course->name     = $request->name;
        $course->description     = $request->description;
        $course->content     = $request->content;
        $course->img1       = $img1;
        $course->img2       = $img2;
        $course->img3       = $img3;
        $course->img4       = $img4;
        $course->save();
        return $course;
    }

    public function update($id, array $data)
    {
        $course = Project::find($id);
        if (! $course) {
            return response()->json(['error' => 'Project  not found'], 404);
        }
        // Only update fields that are provided
        $course->update($data);
        return $course;
    }

    public function delete($id)
    {
        $city = Project::find($id);
        if (! $city) {
            return null; // or handle the case where doctor is not found
        }
        $city->update(['is_delete' => 1]);
        return $city;
    }

}
