<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class TailorProfileController extends BaseController
{
    /**
     * Get tailor shop profile
     */
    public function getProfile()
    {
        try {
            $user = Auth::user();
            $user->load('specializations');

            return $this->sendResponse([
                'id' => $user->id,
                'name' => $user->name,
                'phone_number' => $user->phone_number,
                'address' => $user->address,
                'shop_description' => $user->shop_description,
                'profile_photo' => $user->profile_photo ? asset('storage/' . $user->profile_photo) : null,
                'specializations' => $user->specializations->map(function($spec) {
                    return [
                        'id' => $spec->id,
                        'name' => $spec->name
                    ];
                })
            ], 'Data profil toko berhasil diambil');
        } catch (\Exception $e) {
            return $this->sendError('Error.', ['error' => 'Terjadi kesalahan saat mengambil data profil'], 500);
        }
    }

    /**
     * Update tailor shop profile
     */
    public function updateProfile(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'name' => 'required|string|max:255',
                'phone_number' => 'required|string|max:15',
                'address' => 'required|string|max:500',
                'shop_description' => 'nullable|string|max:1000',
                'profile_photo' => 'nullable|image|mimes:jpeg,png,jpg|max:2048'
            ]);

            if ($validator->fails()) {
                return $this->sendError('Error validasi.', $validator->errors(), 422);
            }

            $user = Auth::user();

            // Handle profile photo upload
            if ($request->hasFile('profile_photo')) {
                // Delete old photo if exists
                if ($user->profile_photo) {
                    Storage::disk('public')->delete($user->profile_photo);
                }

                // Store new photo
                $path = $request->file('profile_photo')->store('profile_photos', 'public');
                $user->profile_photo = $path;
            }

            // Update user data
            $user->name = $request->name;
            $user->phone_number = $request->phone_number;
            $user->address = $request->address;
            $user->shop_description = $request->shop_description;
            $user->save();

            // Reload user with specializations
            $user->load('specializations');

            return $this->sendResponse([
                'id' => $user->id,
                'name' => $user->name,
                'phone_number' => $user->phone_number,
                'address' => $user->address,
                'shop_description' => $user->shop_description,
                'profile_photo' => $user->profile_photo ? asset('storage/' . $user->profile_photo) : null,
                'specializations' => $user->specializations->map(function($spec) {
                    return [
                        'id' => $spec->id,
                        'name' => $spec->name
                    ];
                })
            ], 'Profil toko berhasil diupdate');
        } catch (\Exception $e) {
            return $this->sendError('Error.', ['error' => 'Terjadi kesalahan saat mengupdate profil'], 500);
        }
    }

    /**
     * Delete profile photo
     */
    public function deleteProfilePhoto()
    {
        try {
            $user = Auth::user();

            if ($user->profile_photo) {
                Storage::disk('public')->delete($user->profile_photo);
                $user->profile_photo = null;
                $user->save();
            }

            return $this->sendResponse(null, 'Foto profil berhasil dihapus');
        } catch (\Exception $e) {
            return $this->sendError('Error.', ['error' => 'Terjadi kesalahan saat menghapus foto profil'], 500);
        }
    }
}
