import uuid
import json
from flask import Flask, request, jsonify
from flask_cors import CORS


class Resume:
    def __init__(self, uuid, full_name, skills, experience, phone, email):
        self.uuid = uuid
        self.full_name = full_name
        self.skills = skills
        self.experience = experience
        self.phone = phone
        self.email = email


app = Flask(__name__)
CORS(app)

resumes = {}


@app.route("/resumes", methods=["GET"])
def get_resumes():
    return jsonify([resume.__dict__ for resume in resumes.values()])


@app.route("/resumes/<resume_uuid>", methods=["GET"])
def get_resume(resume_uuid):
    resume = resumes.get(resume_uuid)
    if resume:
        return jsonify(resume.__dict__)
    else:
        return jsonify({"error": "Resume not found"}), 404


@app.route("/resumes", methods=["POST"])
def create_resume():
    data = request.get_json()
    resume_uuid = str(uuid.uuid4())
    resume = Resume(
        resume_uuid,
        data["full_name"],
        data["skills"],
        data["experience"],
        data["phone"],
        data["email"],
    )
    resumes[resume_uuid] = resume
    return jsonify(resume.__dict__), 201


@app.route("/resumes/<resume_uuid>", methods=["PUT"])
def update_resume(resume_uuid):
    resume = resumes.get(resume_uuid)
    if resume:
        data = request.get_json()
        resume.full_name = data.get("full_name", resume.full_name)
        resume.skills = data.get("skills", resume.skills)
        resume.experience = data.get("experience", resume.experience)
        resume.phone = data.get("phone", resume.phone)
        resume.email = data.get("email", resume.email)
        return jsonify(resume.__dict__)
    else:
        return jsonify({"error": "Resume not found"}), 404


@app.route("/resumes/<resume_uuid>", methods=["DELETE"])
def delete_resume(resume_uuid):
    resume = resumes.get(resume_uuid)
    if resume:
        del resumes[resume_uuid]
        return "", 200
    else:
        return jsonify({"error": "Resume not found"}), 404


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=5001)