<?php

namespace App\Services\Dashboard;

use App\Helper\Media;
use App\Models\Team;
use App\Services\JsonTranslationService;
use Illuminate\Support\Facades\DB;

class TeamService
{
    public function store($request)
    {
        DB::beginTransaction();
        try {
            $data = $request->validated();

            // Prepare main data (non-translatable fields)
            $mainData = [
                'img' => null,
                'status' => $data['status'] ?? 1,
                'show_in_home' => $data['show_in_home'] ?? 0,
                'order' => $data['order'] ?? 0,
            ];

            // Handle file upload
            if ($request->hasFile('img')) {
                $mainData['img'] = Media::uploadAndAttachImageStorage($request->file('img'), 'teams');
            }

            // Get translation fields
            $translationFields = ['name', 'job_title'];

            // Create model with JSON translations
            $team = JsonTranslationService::createWithTranslations(Team::class, $mainData, $request, $translationFields);

            DB::commit();
            return $team;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function update($request, $team)
    {
        $data = $request->validated();

        DB::beginTransaction();
        try {
            // Prepare main data (non-translatable fields)
            $mainData = [
                'status' => $data['status'] ?? $team->status,
                'show_in_home' => $data['show_in_home'] ?? $team->show_in_home,
                'order' => $data['order'] ?? $team->order,
            ];

            // Handle file upload
            if ($request->hasFile('img')) {
                $oldImg = $team->getRawOriginal('img');
                if ($oldImg) {
                    Media::removeFile('teams', $oldImg);
                }
                $mainData['img'] = Media::uploadAndAttachImageStorage($request->file('img'), 'teams');
            }

            // Get translation fields
            $translationFields = ['name', 'job_title'];

            // Update model with JSON translations
            JsonTranslationService::updateWithTranslations($team, $mainData, $request, $translationFields);

            DB::commit();
            return $team;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function delete($selectedIds)
    {
        $teams = Team::whereIn('id', $selectedIds)->get();

        DB::beginTransaction();
        try {
            foreach ($teams as $team) {
                // Delete associated image if it exists
                $img = $team->getRawOriginal('img');
                if ($img) {
                    Media::removeFile('teams', $img);
                }
            }
            $deleted = Team::whereIn('id', $selectedIds)->delete();

            DB::commit();
            return $deleted > 0;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }
}

